tags:

views:

59

answers:

4

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+

+5  A: 

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 1234-1234567:

\b\w{8}-\w{22}\b
Kobi
almost, except that \w also matches underscores
unbeli
+1, and I'd also recommend surrounding everything with `\b` tags just to be sure.
Tim Pietzcker
@Tim - Excellent point, better than my comment about `^..$`.
Kobi
+1  A: 

You want to use

\w{8}-\w{22}

In most regex languages, \w will match a word character.

Evan Trimboli
+1  A: 

Maybe \w{8}-\w{22} ?

rybz
+1  A: 

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

ufotds