0

Please help in developing a regEx expression. My requirement is to validate the following string,

String="asdasd|adajjsd|asas,asdas|asda|sd";

pattern id [String]separatedByPipe[String]separatedByPipe[String]

ex:String="asdasd|adajjsd|asas".

The above pattern is repeated, separated by comma, if there are multiple Strings ex:String="asdasd|adajjsd|asas,asdas|asda|sd";

I can make an assumption that the pattern is going to repeat only max 3 times, ie at max 2 commas in my String.

Attached my testClass

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestReg {

public static void main(String[] args) {

    //String regex = "[a-z]+\\|[a-z]+,[a-z]+\\|[a-z]*";
    String regex = "([a-z]\\|[a-z]\\|[a-z]\\,?)+";
    //String regex = "[a-z]+\\|re{ 1}[a-z]+\\|re{ 1}[a-z]+[a-z]+\\|[a-z]+\\|[a-z]*";
    //String regex = "[a-z](|)[a-z](,)[a-z](|)[a-z]";
    //String pattern="([a-z]+@)([a-z]+)(\\.[a-z]+)"; 
    //String inputString1 = "shdifhsdui-asdasd,shdifhsdui|abc";


    //String inputString1 = "";//failed
    String inputString1 = "asdasd|adajjsd|asas,asdas|asda|sd";//success
    //String inputString1 = "asdasd|adasd-asdas|asdasd";//success
    //String inputString1 = "asdasd|adasd|asassa,asdas|asdasd";//failed
    Pattern pattern1 = Pattern.compile(regex);
    Matcher matcher1 = pattern1.matcher(inputString1);
    boolean result = matcher1.find();          
    System.out.println(result);
}

}

hi all ,

System.out.println ("asdasd|adajjsd|asa,s|sad|sdas,asd|as|as|da".matches ("([a-z]*\\|[a-z]*\\|[a-z]*\\,?)+"));

working like charm. So why the same is not working in my above class. Kindly help.Am using jdk1.5

1
  • You do not need to backslash-escape a , as it has no special meaning in regex. Commented May 31, 2013 at 9:50

6 Answers 6

1

This java regex will match your requirements:

"[a-z]+(\\|[a-z]+){2}(,[a-z]+(\\|[a-z]+){2})*"

That will validate the input, but to extract the parts I would simply use split() on commas, then for each part split on pipe:

for (String group : input.split(",")) {
    String[] parts = group.split("\\|");
    // do something with the parts
}

It's just two lines of code to get to your data.

Sign up to request clarification or add additional context in comments.

3 Comments

hi this is failing for the below string. String inputString1 = "asdasd|adajjsd|asas,asdas|asda|sd|"; I have added an extra pipe at the last.I am not in a situation to use the split method.
Your answer is correct if using ".matches()" method.I have edited the question. Please have a look. matcher() and matches(), giving different outputs.
I can get split to work, but with your example, are you expecting a blank field at the end?
0

change the String regex to

String regex = "([a-z]*\\|[a-z]*\\|[a-z]*\\,?)+";

and then try

    String inputString1 = "asdasd|adajjsd|asas,asdas|asda|sd";
    String regex = "([a-z]*\\|[a-z]*\\|[a-z]*\\,?)+";
    Pattern pattern1 = Pattern.compile(regex);
    Matcher matcher1 = pattern1.matcher(inputString1);
    boolean result = matcher1.find();          

    System.out.println(inputString1.substring(matcher1.regionStart(), matcher1.regionEnd()));

2 Comments

hi this is failing for the below string. String inputString1 = "asdasd|adajjsd|asas,asdas|asda|sd|"; I have added an extra pipe at the last.
I have edited the question. Please have a look. matcher() and matches(), giving different outputs.
0

Split on ,, then on |

String[] terms = string.split(",");
for (String term : terms) {
    String[] parts = term.split("\\|");
}

1 Comment

I have edited the question. Please have a look. matcher() and matches(), giving different outputs.
0

Your can validate it using this regex

^(?!.*,$)[a-z]+(\\|[a-z]+,?)*$

2 Comments

@SujeeshSValath you should specify all the valid and invalid combinations in your question..else no one would be able to help you
I have edited the question. Please have a look. matcher() and matches(), giving different outputs.
0

if you want to test above entire static string as one string then this regex pattern will help you .. but if you want to test the different string seprated by "|" then you need to take other regex pattern.

String regex = "([a-z]{6}\|[a-z]{7}\|[a-z]{4}\,[a-z]{5}\|[a-z]{2}?)+";

1 Comment

hi this is failing for the below string. String inputString1 = "asdasd|adajjsd|asas,asdas|asda|sd|"; I have added an extra pipe at the last.
0

Please try if this helps:

String regex = "[a-z]*\\|*,*[a-z]*";

And rest of the code should not need change.

4 Comments

failing for String inputString1 = "asdasd|adajjsd|asas,asdas|asda|sd|"
Please try this String regex = "[a-z]*\\|*,[a-z]*\\|";. Also please specify the combination that are possible from your requirements perspective. I go by Anirudh's point on this.
I have edited the question. Please have a look. matcher() and matches(), giving different outputs.
Pattern you are trying to match in your code is: one character|one character|one character,one character|one character|one character When you put * after [a-z] it matches 0 or more occurence of the character so it works when you use matches of String class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.