How To get range of Value with Regular expression

  • Thread starter Thread starter slesaint
  • Start date Start date
S

slesaint

Hi all,

Langage : .NET
Framework : 1.1
OS: Win XP 2000 Pro

I have this regex :

^*FOO([1-9]|1[0-3])

Which must to match with : 01_FOO1, 01_FOO10, 01_FOO13 and not to
01_FOO14, 01_FOO0

But this regex match with 01_FOO1 and 01_FOO14, but if i need to
01_FOO14 ( which
it'snt in the wanted range, it match because 01_FOO1 match the pattern.

How to do this ?

Thanks for reply
 
I have this regex :

^*FOO([1-9]|1[0-3])

Which must to match with : 01_FOO1, 01_FOO10, 01_FOO13 and not to
01_FOO14, 01_FOO0

But this regex match with 01_FOO1 and 01_FOO14, but if i need to
01_FOO14 ( which
it'snt in the wanted range, it match because 01_FOO1 match the pattern.

How to do this ?

When you get a match against the first term in your alternation, [1-9], you
need to ensure that the following character is not a digit greater than 3.
Since you don't want (usually) to capture the following character in this
case you need to use a zero-width, negative, lookahead assertion. Something
like this:

"^.*FOO(1[0-3]|[1-9](?![0-9]))"

This says; first, try to match the 10, 11, 12, or 13. But if not, then
match any single digit as long as the character following that digit is not,
itself, a digit. Because we use a lookahead assertion, that following
character is not part of the captured text.

This should match FOO10, FOO11, FOO12, FOO13, and any of FOO1, FOO2,...,
FOO9, but not FOO0, FOO14, FOO42, etc.

Note: I'm entering this just off the top of my head, and there may be
problems with it, but I believe this is the type of approach you will need.

-- Tom
 
: I have this regex :
:
: ^*FOO([1-9]|1[0-3])

I assume you mean ^.*FOO([1-9]|1[0-3]) -- i.e., caret, dot-star.

: Which must to match with : 01_FOO1, 01_FOO10, 01_FOO13 and not to
: 01_FOO14, 01_FOO0
:
: But this regex match with 01_FOO1 and 01_FOO14, but if i need to
: 01_FOO14 (which it'snt in the wanted range, it match because 01_FOO1
: match the pattern.

Is there anything else in the input?

If these FOO values are all that's there, then anchor your pattern
on the end with $ to prevent 01_FOO14 from matching. Otherwise,
bookend your pattern with whatever follows.

Greg
 
Thanks for your reply.

Effecively Tom, I did'nt think by the negative. I note for the later.
This approach is, in my case, effective and elegant.

I has test it and that works.

Thanks, and have fun code with .NET
Le Saint
 
Back
Top