Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Regex Help

Status
Not open for further replies.

Laeg

Programmer
Nov 29, 2004
95
0
0
IE
I'm trying to capture the following

'The digit 0 followed by 1-3 occurences of any digit and then a hyphen character followed by 5-7 occurences of any digit

So like 025-23456, 057-3456789, 086-458953 etc

Dim MyRegEx As New Regex("/^0\[0-9]{1,3}-\[0-9]{5,7}/")

Does not seem to be matching for me
 
Yeah, agree with glyns.

Thr only things you had wrong were that you don't need / to bracket the expression and you don't need to prefix character classes (the characters contained between []) with a \.

Cheers

Andy
---------------------------------
[green]' Signature removed for testing purposes.[/green]

 
Seems good alright but it fails on this case here

"a hyphen character followed by 5-7 occurences of any digit"

as if I try more than 7 characters after the hyphen the expression matches. Presumably the expression

0[0-9]{1,3}-[0-9]{5,7}

reads like 0 followed by 1-3 more digits followed by a hyphen followed by 5-7 digits which will match something
that has 8 digits after the hyphen as it does satisfy the 5-7 digits but just happens to have an eight too! What is the syntax for extending the last part of the expression to say a hyphen followed by 5-7 digits only?


 
As you want only matches (the current expression will match the 025-23456 portion of 3453fwer025-23456) that are exact, rather than extracting matches from a larger text you might want to add the caret back in at the start to match the start of the string:
Code:
^0[0-9]{1,3}-[0-9]{5,7}$
Hope this helps

Andy
---------------------------------
[green]' Signature removed for testing purposes.[/green]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top