rubyの遡及正規表現

2725 ワード


password =~ /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,40}$/
#(?=.*\d) any thing with a number
#at least one number one up case one down case


^ - Matches beginning of line
$ - Matches end of line
\A - Matches beginning of string.
\Z - Matches end of string. If string ends with a newline, it matches just before newline
\z - Matches end of string
\G - Matches point where last match finished
\b - Matches word boundaries when outside brackets; backspace (0x08) when inside brackets
\B - Matches non-word boundaries
(?=pat) - Positive lookahead assertion: ensures that the following characters match pat, but doesn't include those characters in the matched text
(?!pat) - Negative lookahead assertion: ensures that the following characters do not match pat, but doesn't include those characters in the matched text
(?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text
(?<!pat) - Negative lookbehind assertion: ensures that the preceding characters do not match pat, but doesn't include those characters in the matched text
# If a pattern isn't anchored it can begin at any point in the string

/real/.match("surrealist") #=> #<MatchData "real">
# Anchoring the pattern to the beginning of the string forces the
# match to start there. 'real' doesn't occur at the beginning of the
# string, so now the match fails
/\Areal/.match("surrealist") #=> nil
# The match below fails because although 'Demand' contains 'and', the
pattern does not occur at a word boundary.
/\band/.match("Demand")
# Whereas in the following example 'and' has been anchored to a
# non-word boundary so instead of matching the first 'and' it matches
# from the fourth letter of 'demand' instead
/\Band.+/.match("Supply and demand curve") #=> #<MatchData "and curve">
# The pattern below uses positive lookahead and positive lookbehind to
# match text appearing in <b></b> tags without including the tags in the
# match
/(?<=<b>)\w+(?=<\/b>)/.match("Fortune favours the <b>bold</b>")
    #=> #<MatchData "bold">

http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm