Ruby Regexp: A, z, ^ and $

Posted on
string = "my dog isn5 years old"

string[/Ad+/] # match a sequence of digits at the start of the string
# => nil
string[/^d+/]  # match a sequence of digits at the start of a line
# => "5"

string[/w+z/] # match a sequence of characters at the ending of the string
# => "old"
string[/w+$/]  # match a sequence of characters at the ending of a line
# => "is"

Leave a Reply

Your email address will not be published.