Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Understanding a RegEx expression
Message
From
11/03/2014 15:03:46
 
 
To
11/03/2014 13:40:09
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01596120
Message ID:
01596138
Views:
53
This message has been marked as a message which has helped to the initial question of the thread.
>There is something I am trying to understand with a RegEx expression.
>
>Here is an expression: ([A-Za-z0-9-]{1,20})-([A-Za-z0-9]{1,12})
>
>That one allows alphanumeric characters from anywhere from the 1st to the 20th character. This includes as well a dash starting from position 2.
>
>This is the part I cannot understand. What in there is a representation that the dash is valid from position 2?

You are misinterpreting the {1,20} and {1,12}. These mean that the group preceding it is repeated between 1 and 20 or 1 and 12 times respectively. The expression you probably want is: ([A-Za-z0-9][A-Za-z0-9\-]{19})\-([A-Za-z0-9]{12})

To break this expression down:
The first [A-Za-z0-9] indicates the first character must be A-Z, a-z, or 0-9.
The following [A-Za-z0-9\-]{19} indicates the next 19 characters must be A-Z, a-z, 0-9, or -.
The parenthesis around these two make it into a capturing group.
The next \- matches a literal hyphen.
The final ([A-Za-z0-9]{12}) matches 12 characters of A-Z, a-z, or 0-9.

If any of the blocks can be variable length, change the {n} to {n,m} where n is the least number of times that block can repeat, and m is the most it can repeat. http://www.regular-expressions.info/refrepeat.html also provides more information on the variations of the repeating indicator.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform