Reputation: 11782
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
Upvotes: 0
Views: 268
Reputation: 7532
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
Upvotes: 2
Reputation: 26012
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
)
Upvotes: 0