Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby regex: match and get position(s) of

Tags:

regex

ruby

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?

like image 766
Austin Richardson Avatar asked Mar 09 '11 05:03

Austin Richardson


People also ask

What does =~ mean in Ruby regex?

=~ 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.

How do you use the Match function in Ruby?

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.

How do you match expressions in regex?

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).


3 Answers

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] 
like image 130
Sean Hill Avatar answered Oct 15 '22 00:10

Sean Hill


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] 
like image 38
DigitalRoss Avatar answered Oct 15 '22 00:10

DigitalRoss


"AustinTexasDallasTexas".gsub(/Texas/).map { Regexp.last_match.begin(0) }
  #=> [6, 17]
like image 34
Cary Swoveland Avatar answered Oct 15 '22 01:10

Cary Swoveland