473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Regex query

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
May 24 '07 #1
5 1561
Mantorok wrote:
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}

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 24 '07 #2
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" <ma*******@yahoo.dewrote in message
news:eH****************@TK2MSFTNGP04.phx.gbl...
Mantorok wrote:
>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}

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

May 24 '07 #3
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

"Mantorok" <no**@none.comwrote in message
news:f3**********@newsfeed.th.ifl.net...
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

May 24 '07 #4
* 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
May 24 '07 #5
"Jesse Houwing" <je***********@nospam-sogeti.nlwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>* 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
May 25 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fazer | last post by:
Hello, I have a string which has a url (Begins with a http://) somewhere in it. I want to detect such a url and just spit out the url. Since I am very poor in regex, can someone show me how to...
3
by: a | last post by:
I'm a newbie needing to use some Regular Expressions in PHP. Can I safely use the results of my tests using 'The Regex Coach' (http://www.weitz.de/regex-coach/index.html) Are the Regular...
3
by: jwgoerlich | last post by:
Hello group, I am working on a query string class. The purpose is to parse name-value pairs from incoming text. Currently, I am using the Regex code below. I have two questions. First, the...
0
by: =?ISO-8859-15?Q?C=E9dric?= | last post by:
Hi all, I want to import a SQL script (SQLite) executing each queries separately. - I read the SQL file - I split the read string with the separator ";" - I execute each query string query...
15
by: nagar | last post by:
I need to split a string whenever a separator string is present (lets sey #Key(val) where val is a variable) and rejoin it in the proper order after doing some processing. Is there a way to use...
0
by: rmccullough | last post by:
The test tool I use produces xml files that I am parsing and inserting into a mysql database. I then have a set of php pages that report on the test results. I wrote a C# console application that...
0
by: Guoqi Zheng | last post by:
Dear Sir, I need to use regex to replace some string. Below is what I use. output = "sample data <href=""xlink:GG44-33"">, part two <href=""xlink:GG55-123"">" Dim regEx Set regEx = New...
0
by: vmysore | last post by:
I am trying to get all the columns selected within a SQL query (including the sub selects). When the code hits matcher.find(). i get the following exception: Exception in thread "main"...
4
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Prior to now, my RegEx expression was as follows: RegEx pattern = new Regex(@"\d{5} \d{4} \d{2}"); That has worked, but now I have been asked to enable the ability for our expressions to...
2
by: Smokey Grindel | last post by:
I basically need to remove any leading zeros and hyphens from a string... regex seemed like the best rout and using a replace regex method... anyone know of any good strings for this? The one I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.