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]
matchesa
orA
(--)?[aA]dd
:(--)?
matches--
or nothing[aA]
matchesa
orA
dd
matchesdd
(x|y)
matchesx
ory
$
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.
精彩评论