Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Understanding a RegEx expression
Message
From
11/03/2014 15:48:43
 
 
To
11/03/2014 15:27:41
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:
01596146
Views:
46
This message has been marked as a message which has helped to the initial question of the thread.
>>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.
>
>Thanks, this is very valuable.
>
>Basically, this is more readable I would assume for someone who tries to interpret this for the first time:
>
>([A-Za-z0-9]{1}[A-Za-z0-9\-]{19})\-([A-Za-z0-9]{12})
>
>So, at least, in the first part, we know it is only for character 1. Then, we have another filter for the next 19 characters.
>
>Or:
>
>([A-Za-z0-9]{1}[A-Za-z0-9\-]{19})-([A-Za-z0-9]{12})
>
>...if we ignore the \ which is only there to make sure the - is interpreted ok.

Yes, either of those should be equivalent. You may want to also add a ^ to the beginning and a $ to the end to ensure the matched string is from a single word and not a substring of a larger word. Depending on what you are doing with the regex, the parenthesis may also be unnecessary. They will store off those substrings as a match that can be retrieved later or used in a replace. If you are using the matching groups, you don't need them.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform