regular expressions

M

Mike P

I have a regular expression that I use on text boxes where I want to
limit the user to letters a-z and spaces. I now need to allow
characters such as ö, ä and å (Nordic characters). Does anybody know
how to do this in a regular expression? Here is my current standard
regular expression :

^[a-zA-Z\s]+$

Any assistance would be really appreciated.


Cheers,

Mike
 
C

Chris R. Timmons

I have a regular expression that I use on text boxes where I
want to limit the user to letters a-z and spaces. I now need to
allow characters such as ö, ä and å (Nordic characters). Does
anybody know how to do this in a regular expression? Here is my
current standard regular expression :

^[a-zA-Z\s]+$

Any assistance would be really appreciated.

Mike,

You can add those Nordic characters to the existing character class:

^[a-zA-Zöäå\s]+$

Alternately, you can match all of the characters in a Unicode
"block". Nordic characters are part of the "Latin-1 Supplement"
block, which are characters in the range of 0080-00FF:

^[a-zA-Z\u0080-\u00FF\s]+$

Unicode code charts can be found here:
http://www.unicode.org/charts/

Where Is My Character?
http://www.unicode.org/standard/where/

Unicode and Regular Expressions:
http://www.unicode.org/unicode/reports/tr18/
 
G

Guest

I also met with a problem with RegEx.

I want to match such pattern "position:12", and get the substring "12" from
the pattern. What I used in C# is:

Regex regex = new Regex( "position:(?<num>)" );

However, when I use the statement:

string result = match.Groups["num"].Value;

in which match is a Match object that is generated by
regex.Match(sourceString); what I got is a NULL string.

Could anyone explain why? Thanks in advance!
 
C

Chris R. Timmons

I also met with a problem with RegEx.

I want to match such pattern "position:12", and get the
substring "12" from the pattern. What I used in C# is:

Regex regex = new Regex( "position:(?<num>)" );

However, when I use the statement:

string result = match.Groups["num"].Value;

in which match is a Match object that is generated by
regex.Match(sourceString); what I got is a NULL string.

The (?<num>) capture group does not have any character matching
specifiers. If you want it to capture a number after the colon,
change it to this:

Regex regex = new Regex( "position:(?<num>\d+)" );
 
G

Guest

Thanks Chris !

You are right and I used "position:(?<num>\\d+)" to get the correct data~

I referred to the MSDN, and learned about Group :)

Chris R. Timmons said:
I also met with a problem with RegEx.

I want to match such pattern "position:12", and get the
substring "12" from the pattern. What I used in C# is:

Regex regex = new Regex( "position:(?<num>)" );

However, when I use the statement:

string result = match.Groups["num"].Value;

in which match is a Match object that is generated by
regex.Match(sourceString); what I got is a NULL string.

The (?<num>) capture group does not have any character matching
specifiers. If you want it to capture a number after the colon,
change it to this:

Regex regex = new Regex( "position:(?<num>\d+)" );
 
J

Jay B. Harlow [MVP - Outlook]

Mike,
In addition to the other comments.

Have you considered "word characters"? the \w escape?

^[\w\s]+$

Which unfortunately includes numbers.

You could try the unicode block character classes (based on the \w escape):

^[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Pc}]. \s]+$

\p{Ll} is all lower case leters & not just a-z...


Expresso & RegEx Workbench both have wizards of varying degrees to help you
build your expression, plus they allow you to test your expressions, also
the analyzer/interpreter in each is rather handy.

Expresso:
http://www.ultrapico.com/Expresso.htm

RegEx Workbench:
http://www.gotdotnet.com/Community/...pleGuid=c712f2df-b026-4d58-8961-4ee2729d7322A

tutorial & reference on using regular expressions:
http://www.regular-expressions.info/

The MSDN's documentation on regular expressions:
http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay
 

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

Top