I want to match this :
- eight(8) alphanumeric characters
- followed by - hyphen
- followed by twentytwo(22) alphanumeric characters, here is what I tried and its not matching :
[8]\w+-[22]\w+
I want to match this :
[8]\w+-[22]\w+
Should be:
\w{8}-\w{22}
[8]
matches a single character - the literal 8, and [22]
matches one literal 2.
Note that \w
also allows the underscore. If that is a problem, use
[a-zA-Z0-9]{8}-[a-zA-Z0-9]{22}
A good tip from Tim, if you want tp capture the pattern from a file or string, you probably want to add \b
- word boundary, to avoid partial matching. For example, if you wanted a 2-4 format 12-1234
, the first parrent will match 12
34-1234
567
:
\b\w{8}-\w{22}\b
You want to use
\w{8}-\w{22}
In most regex languages, \w will match a word character.
Regular expression syntax depends on the language that you use.
[A-Za-z0-9]{8}-[A-Za-z0-9]{22}
Note that \w matches underscores