ruby non greedy regex
text = "The quick brown fox jumps over the lazy dog"
pattern = /o.*?o/
matches = text.scan(pattern)
puts matches.inspect
text = "The quick brown fox jumps over the lazy dog"
pattern = /o.*?o/
matches = text.scan(pattern)
puts matches.inspect
This Ruby code demonstrates the usage of a non-greedy regex pattern. It defines a string text
and a regex pattern pattern
. The pattern /o.?o/
matches any substring that starts with the letter "o", followed by any characters (.?
matches zero or more characters non-greedily), and ends with the next occurrence of the letter "o".
The scan
method is then used on the text
variable with the regex pattern to find all matches. Finally, puts matches.inspect
prints an array containing all the matches found using the non-greedy regex pattern in the given text.