0

I have a sample message . I need to create a regular expression to validate using android pattern.

sample message : ERR|any digit|any digit;

checking validation:

1.Starting fixed characters :ERR

  1. separator character :|

  2. digit after | character

  3. Message termination ;

I have tried like this way:^{ERR}+{|}+\d+{|}+\d+{;}$

Am I right? Please help to solve my problem.

1
  • you should add regex tag to your question you might recieve help Commented Nov 17, 2015 at 6:57

1 Answer 1

1

The corrected regex you gave would be ^(ERR)+(\\|)+\\d+(\\|)+\\d+;$. Brackets are used for grouping, not braces. Also, in regex, + is used to represent "one or more of the previous expression". So writing (ERR)+ means "one or more of the string 'ERR'", so strings like "ERRERR|123|456;" would be matched (same thing goes for the pipe characters) - this is not what you are trying to do, I assume.

Having said that, try this: "^ERR\\|\\d+\\|\\d+;$"

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

2 Comments

The elimirks answer is formally correct (based on what we know), only needs some double escaping: Pattern p = Pattern.compile("^ERR\\|\\d+\\|\\d+;$");
@GsusRecovery Thanks for the correction - I updated it accordingly

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.