Help with Regex

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have this regex

^[a-zA-Z] + (([\'\,\.\-][a-zA-Z])?[a-zA-Z]*)*$

How can I get it to check the length of the string up to 25 characters? so
that any string more than 25 characters will not be matched.

Thanks
 
Chris said:
I have this regex

^[a-zA-Z] + (([\'\,\.\-][a-zA-Z])?[a-zA-Z]*)*$

How can I get it to check the length of the string up to 25 characters? so
that any string more than 25 characters will not be matched.

The simplest way would just be to have a separate (non-regex) check:

if (foo.Length > 25 && [do the regex match here])
{
....
}
 
Jon said:
I have this regex

^[a-zA-Z] + (([\'\,\.\-][a-zA-Z])?[a-zA-Z]*)*$

How can I get it to check the length of the string up to 25 characters? so
that any string more than 25 characters will not be matched.

The simplest way would just be to have a separate (non-regex) check:

if (foo.Length > 25 && [do the regex match here])
{
...
}

I believe this should be "foo.Length <= 25 && [regex match]", logically.

Apart from that, this is not only the simplest but also the only useful
way, really, because regular expressions don't have much support for
length checking. The only construct vaguely associated with that would be
{n,m}, which is a quantifier - a{2,7} would match a string of 2 to 7 a's.
But in your expression there's no place where a quantifier like that could
restrict the match length of the whole expression.


Oliver Sturm
 
Oliver Sturm said:
The simplest way would just be to have a separate (non-regex) check:

if (foo.Length > 25 && [do the regex match here])
{
...
}

I believe this should be "foo.Length <= 25 && [regex match]", logically.

Oops, of course :)
 
Back
Top