Solved

validating comma seperated String

Posted on 2009-05-15
6
343 Views
Last Modified: 2012-05-07
hi guys

I have a question on a string 'environments' which i need to validate.

String environments could be  a1,a2,a3,b1,b2,b3......t1,t2,t3...z1,z2,z3...z9

environments can be single or multiple
If it is single , it can be either
a1 or a2 or a3 or a4 or a5 or a6 or a7 or a8 or a9
OR
b1 or b2 or b3 or ...b9
...
...
OR
z1 or z2 or z3 or...z9



If it is multiple, it can be a combination of above values(seperated by comma). so it can be
something like

a1,a2,c1,c9,t1,t4,z1,z6..


Is there anyway i can validate that if String environments is anything other than the above format(single or multiple), show error


any help greatly appreciated

thanks
J
0
Comment
Question by:jaggernat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 24396720
You could probably split twice: first on delimiters, then on letters and numbers
0
 
LVL 12

Assisted Solution

by:Gibu George
Gibu George earned 150 total points
ID: 24397247
append a , to the string, use regex to match the pattern  <alphabet><numeric><comma> and then replace with empty string, check if the output string is empty

like this
String s="a1,a2,c1,c9,t1,t4,z1,z6.";
s=s.replaceAll("(([a-z])([1-9]),)*", "");
if("".equals(s)){
SOP("valid");
}
0
 
LVL 12

Expert Comment

by:Gibu George
ID: 24397273
don't forget to append a comma to the end
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 12

Expert Comment

by:Gibu George
ID: 24397329
for example if the input is

a1,a23,ab3,b1,,b2,b3,

replaced output will be

a23,a,
0
 
LVL 3

Accepted Solution

by:
hazgoduk earned 300 total points
ID: 24400448
If you want single letter followed by single number (i.e, a1-9, - z1-9 and not a10, a11 etc) use the first pattern to match, otherwise use the 2nd pattern
String[] split = "a1,a2,a3".split(",");
boolean allValid = true;
for(String s : split)
{
    if(!singleDigitOnly(s))
    {
        allValid = false;
        break;
    }
}
 
if(!allValid)
{
    //Do what you want to when one value doesn't match
    System.out.println("not all valid");
}
else
{
    System.out.println("all valid");
}
 
 
public boolean singleDigitOnly(String s)
{
        Matcher m = Pattern.compile("^[a-z][\\d]").matcher(s);
 
        if(s.length() == 2 && m.matches())
        {
            System.out.println(s + ": valid");
            return true;
        }
        else
        {
            System.out.println(s + ": invalid");
            return false;
        }
}
 
public boolean multipleDigits(String s)
{        
        Matcher m = Pattern.compile("^[a-z][\\d]*").matcher(s);
 
        if(m.matches())
        {
            System.out.println(s + ": valid");
            return true;
        }
        else
        {
            System.out.println(s + ": invalid");
            return false;
        }
}

Open in new window

0
 
LVL 10

Author Comment

by:jaggernat
ID: 24487767
thanks guys
sorry for the delayed response.

any help with my next question is greatly appriciated!!

thanks
http://www.experts-exchange.com/Programming/Languages/Java/Q_24443170.html
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question