0

I am trying to make a regular expression but i am newbie in it. And it seems Like i am failing again and again in this..

Kindly if anyone can help me

Basically what i want an expression that tests for the following,

1- Something that starts with abc or def, followed by a number between 0 and 900, then can have anything between that, up until the nearest !!

Any help will be regarded

Best Regards

3
  • Can you give an example? What you mean by "then can have anything between that, up until the nearest!!" ? Commented Aug 12, 2012 at 10:35
  • Add some example you want to catch. The then ... part is hard to understand Commented Aug 12, 2012 at 12:28
  • @Yul ignore it, tell me about rest part please. Commented Aug 12, 2012 at 13:21

2 Answers 2

2

If you want this abcXXX(0<xxx<900) or defXXX (0<xxx<900) try this:

\b(abc)([0-9]|[1-9][0-9]|[1-8][0-9][0-9]|900)\b|\b(def)([0-9]|[1-9][0-9]|[1-8][0-9[0-9]|900)\b]

Explanation: The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That's the easy part. So 0-900 is 0-899 and 900 so REGEX is [0-9]|[1-9][0-9]|[1-8][0-9][0-9]|900 . Add \b( )\b is Boundary Matchers. Similar to def : start with def followed by 3 digits.

\b(def)([0-9]|[1-9][0-9]|[1-8][0-9][0-9]|900)\b

At last use | is or.

Tested with Regular Expression Test Page for Java

Maybe i didn't try some weird input but this is the basic parts for you to dig by yourself

Edit with Alan Moore's nicer suggestion :

\b(abc|def)([0-9]|[1-9][0-9]|[1-8][0-9][0-9]|900)\b
Sign up to request clarification or add additional context in comments.

1 Comment

+1, but yu don't have to repeat the whole regex. This will do: \b(abc|def)([0-9]|[1-9][0-9]|[1-8][0-9][0-9]|900)\b
0

The following regex will match with the first part of your expression. It is hard to understand second part of the expression.

Something that starts with abc or def, followed by a number between 0 and 900

^(abc|def)([0-9]{1,2}|[1-8][0-9]{2}|900)

I explained the expressions below.

^(abc|def)   //Starts with abc or def
(
   [0-9]{1,2}|[1-8][0-9]{2}|900   // matches number between 0-900
)

Comments

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.