In Python I can do this:
import re
regex = re.compile('a')
regex.match('xay',1) # match because string starts with 'a' at 1
regex.match('xhay',1) # no match because character at 1 is 'h'
However in Ruby, the match method seems to match everything past the positional argument. For instance, /a/.match('xhay',1) will return a match, even though the match actually starts at 2. However, I want to only consider matches that start at a specific position.
How do I get a similar mechanism in Ruby? I would like to match patterns that start at a specific position in the string as I can in Python.
/^.{1}a/
for matching a at location x+1 in the string
/^.{x}a/
--> DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With