Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match all occurrences of a regex

Tags:

regex

ruby

Is there a quick way to find every match of a regular expression in Ruby? I've looked through the Regex object in the Ruby STL and searched on Google to no avail.

like image 332
Chris Bunch Avatar asked Sep 17 '08 05:09

Chris Bunch


People also ask

How do I find all matches in regex?

The method str. match(regexp) finds matches for regexp in the string str . If the regexp has flag g , then it returns an array of all matches as strings, without capturing groups and other details. If there are no matches, no matter if there's flag g or not, null is returned.

How do I match a range in regex?

Regular-Expressions.info has examples for 1 or 2 digit ranges, but not something that spans both: The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. Something like ^[2-9][1-6]$ matches 21 or even 96!

How do you find how many occurrences of a regex pattern were replaced in a string?

To count a regex pattern multiple times in a given string, use the method len(re. findall(pattern, string)) that returns the number of matching substrings or len([*re. finditer(pattern, text)]) that unpacks all matching substrings into a list and returns the length of it as well.

What is regex quantifier?

Quantifiers specify how many instances of a character, group, or character class must be present in the input for a match to be found.


2 Answers

Using scan should do the trick:

string.scan(/regex/) 
like image 92
Jean Avatar answered Sep 28 '22 03:09

Jean


To find all the matching strings, use String's scan method.

str = "A 54mpl3 string w1th 7 numb3rs scatter36 ar0und" str.scan(/\d+/) #=> ["54", "3", "1", "7", "3", "36", "0"] 

If you want, MatchData, which is the type of the object returned by the Regexp match method, use:

str.to_enum(:scan, /\d+/).map { Regexp.last_match } #=> [#<MatchData "54">, #<MatchData "3">, #<MatchData "1">, #<MatchData "7">, #<MatchData "3">, #<MatchData "36">, #<MatchData "0">] 

The benefit of using MatchData is that you can use methods like offset:

match_datas = str.to_enum(:scan, /\d+/).map { Regexp.last_match } match_datas[0].offset(0) #=> [2, 4] match_datas[1].offset(0) #=> [7, 8] 

See these questions if you'd like to know more:

  • "How do I get the match data for all occurrences of a Ruby regular expression in a string?"
  • "Ruby regular expression matching enumerator with named capture support"
  • "How to find out the starting point for each match in ruby"

Reading about special variables $&, $', $1, $2 in Ruby will be helpful too.

like image 26
sudo bangbang Avatar answered Sep 28 '22 01:09

sudo bangbang