I'd like to match a regex and get the position in the string of the match
For example,
"AustinTexasDallasTexas".match_with_posn /(Texas)/
I'd like match_with_posn
to return something like: [6, 17]
where 6 and 17 are the start positions for both instances of the word Texas.
Is there anything like this?
=~ is Ruby's basic pattern-matching operator. When one operand is a regular expression and the other is a string then the regular expression is used as a pattern to match against the string. (This operator is equivalently defined by Regexp and String so the order of String and Regexp do not matter.
Ruby | Regexp match() function Regexp#match() : force_encoding?() is a Regexp class method which matches the regular expression with the string and specifies the position in the string to begin the search. Return: regular expression with the string after matching it.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
Using Ruby 1.8.6+, you can do this:
require 'enumerator' #Only for 1.8.6, newer versions should not need this. s = "AustinTexasDallasTexas" positions = s.enum_for(:scan, /Texas/).map { Regexp.last_match.begin(0) }
This will create an array with:
=> [6, 17]
Sort of, see String#index
"AustinTexasDallasTexas".index /Texas/ => 6
Now, you could extend the String API.
class String def indices e start, result = -1, [] result << start while start = (self.index e, start + 1) result end end p "AustinTexasDallasTexas".indices /Texas/ => [6, 17]
"AustinTexasDallasTexas".gsub(/Texas/).map { Regexp.last_match.begin(0) }
#=> [6, 17]
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