Regex query

M

Mantorok

Hi all

I want to clarify a Regex, I need to allow up to 20 digits, spaces are
allowed within the string too, as long as the overall string is not above 20
in length..

Here's is what I have: [\d| ]{1,20}

Is this right or wrong?

Thanks
Kev
 
M

Martin Honnen

Mantorok said:
I want to clarify a Regex, I need to allow up to 20 digits, spaces are
allowed within the string too, as long as the overall string is not above 20
in length..

Here's is what I have: [\d| ]{1,20}

Is this right or wrong?

Why that bar | inside the square brackets? I think you simply want
[\d ]{1,20}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

The bar is to split between numbers "\d" and the space " "

OP:
Just try it, it should be very simply to test

Martin Honnen said:
Mantorok said:
I want to clarify a Regex, I need to allow up to 20 digits, spaces are
allowed within the string too, as long as the overall string is not above
20 in length..

Here's is what I have: [\d| ]{1,20}

Is this right or wrong?

Why that bar | inside the square brackets? I think you simply want
[\d ]{1,20}
 
K

Kevin Spencer

A character class [...] does not require an alternation character ('|'),
just a string of characters, as in:

[\d ]{1, 20}

However, this isn't goint to stop a match at 20 characters, unless you add
an "end of string" assertion:

[\d ]{1, 20}$

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
J

Jesse Houwing

* Kevin Spencer wrote, On 24-5-2007 21:13:
A character class [...] does not require an alternation character ('|'),
just a string of characters, as in:

[\d ]{1, 20}

However, this isn't goint to stop a match at 20 characters, unless you add
an "end of string" assertion:

[\d ]{1, 20}$

And you might want to add a begin string assertion as well. Otherwise
you only have a string which ends with up to 20 digits.

By the way, be wary of the \d shortcut. It has different meanings
dependent on your Locale. In other languages it will also allow other
unicode characters which represent numbers. To work around this use
[0-9] or set the ECMAScript compatibility for your regex class (that
ignores locales as far as I know).

So in the end you would have:

^[0-9 ]{1,20}$

^ Beginning of the string
[0-9 ]{1,20} 1 to 20 characters from this class (you don't need the | as
pointed out before).
$ End of the string.

Jesse Houwing
 
M

Mantorok

Jesse Houwing said:
* Kevin Spencer wrote, On 24-5-2007 21:13:
A character class [...] does not require an alternation character ('|'),
just a string of characters, as in:

[\d ]{1, 20}

However, this isn't goint to stop a match at 20 characters, unless you
add an "end of string" assertion:

[\d ]{1, 20}$

And you might want to add a begin string assertion as well. Otherwise you
only have a string which ends with up to 20 digits.

By the way, be wary of the \d shortcut. It has different meanings
dependent on your Locale. In other languages it will also allow other
unicode characters which represent numbers. To work around this use [0-9]
or set the ECMAScript compatibility for your regex class (that ignores
locales as far as I know).

So in the end you would have:

^[0-9 ]{1,20}$

^ Beginning of the string
[0-9 ]{1,20} 1 to 20 characters from this class (you don't need the | as
pointed out before).
$ End of the string.

Thank you, this works fine now, I also forgot to mention this was for a
piece of client-side javascript, using \d fails, but using 0-9 works fine.

Kev
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top