开发者

Regular expression and repeating character classes in perl

I am trying to write a regular expression that can extract (possibly multiple) strings of four hexadecimal numbers/letters.

I could do something like this: /^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]/

but is there a better way?

It seems like the repeat operator:

a{n} Matches 'a' repeated exactly n times.

a{n,} Matches 'a' repeated n or more times.

a{n, m} Matches 'a' repeated between n and m times inclusive.

Would work, but the following regular expression does not seem to work:

/^[A-Fa-f0-9]{4}+/

I'm trying to match strings like:

AA00

AA00FFAA

0011FFAA0022

and so on. Each string will be on it's开发者_运维问答 own line.

Thanks!


Try this:

/^(?:[A-Fa-f0-9]{4})+$/


You have nested quantifiers in regex; ie, {4} means to match exactly 4 times and + means to match that string many times, so these two quantifiers conflict. If you simply remove the +, it'll work:

/^[A-Fa-f0-9]{4}/
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜