开发者

Need help figuring out a regular expression for these strings

开发者 https://www.devze.com 2023-03-24 12:24 出处:网络
I\'m just learning regex and I\'ve been trying to figure out a pattern which only matches the following 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

精彩评论

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