开发者

Need help figuring out a regular expression for these strings

I'm just learning regex and I've been trying to figure out a pattern which only matches the following strings:

-a
-A
--add
--Add
a
A
add
Ad开发者_JS百科d

I'm using this in Perl if it matters. If you answer, could you explain your regex so I can try and learn what I've been doing wrong?


^(-?[aA]|(--)?[aA]dd)$
  • ^ matches the beginning of the string
  • -?[aA]:
    • -? matches - or nothing
    • [aA] matches a or A
  • (--)?[aA]dd:
    • (--)? matches -- or nothing
    • [aA] matches a or A
    • dd matches dd
  • (x|y) matches x or y
  • $ matches the end of the string

You should invest some time carefully reading http://perldoc.perl.org/perlrequick.html. I still need a reference handy when I write regular expressions.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜