开发者

Regex to match 4 groups of letters/numbers, separated by hyphens

I need a regular e开发者_如何学JAVAxpression that will match this pattern (case doesn't matter):

066B-E77B-CE41-4279

4 groups of letters or numbers 4 characters long per group, hyphens in between each group.

Any help would be greatly appreciated.


^(?:\w{4}-){3}\w{4}$

Explanation:

    ^                  # must match beginning of string
     (?:               # make a non-capturing group (for duplicating entry)
       \w{4}           # a-z, A-Z, 0-9 or _ matching 4 times
       -               # hyphen
     ){3}              # this group matches 3 times
     \w{4}             # 4 more of the letters numbers or underscore
    $                  # must match end of string

Would be my best bet. Then you can use Regex Match (static).

P.S. More info on regex can be found here.

P.P.S. If you don't want to match underscores, the \w above can be replaced (both times) with [a-zA-Z0-9] (known as a class matching lowercase and uppercase letters and numbers). e.g.

^(?:[a-zA-Z0-9]{4}-){3}[a-zA-Z0-9]{4}$


Try:

[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}\-[A-Za-z0-9]{4}


With such a small sample of data, it's not easy to be certain what you actually want.

I'm going to assume that all the characters in that string are hex digits, and that's what you need to search for.

In that case, you would need a regular expression something like this:

^[a-f0-9]-[a-f0-9]-[a-f0-9]-[a-f0-9]$

If they can be any letter, then replace the fs with zs.

Oh, and use myRE.IgnoreCase = True to make it case insensitive.

If you need further advice on regular expressions, I'd recommend http://www.regular-expressions.info/ as good site. They even have a VB.net-specific page.


Assuming from your example:

  • There are four groups of letters, separated by dashes.
  • Each group is four letters.
  • The letters are hexadecimal digits.

This pattern would match that:

^[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{4}-[\dA-F]{4}$

Note that ^ and $ match the beginning and end of the string, which is important if you want to match the entire string and not check if the pattern occurs inside a string.

You could also make use of the repetitions in the pattern:

^(?:[\dA-F]{4}-){3}[\dA-F]{4}$
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜