On Sat, Mar 31, 2012 at 10:17 PM, gabe gabriellini [email protected] wrote:
puts “Mike Lan move the string abc def”.gsub(/(\A|\s)(.{3})($|\s)/,"
new_value ")
it only replaces two same as the next one only replaces every other one
what a im missing??
The ending pattern ($|\s) is consuming what would be matched by the
leading pattern (\A|\s) every other match. Making the first part
optional will work:
/(\A|\s)?(.{3})($|\s)/
If you know the pattern of what you trying to match between the
spaces, for example alphabet only, use a more explicit pattern instead
of .{3}. That way you wouldn’t have to deal with the spaces, only what
you want to replace.
On Sat, Mar 31, 2012 at 11:09 PM, gabe gabriellini [email protected] wrote:
thank you Ammar
You’re welcome!
The only problem i have is when i use a string like this one:
puts “gabriel y martha iba gtz ibanez nat”.gsub(/(\A|\s)([a-zA-Z]{3})
($|\s)/," Gutierrez ")
the gtz after the iba does not get replaced
This is the same problem i mentioned. The pattern matches:
the beginning of line (\A) or a space (\s)
followed by 3 alphabet characters ([a-zA-Z]{3})
followed by end of line ($) or a space (\s)
When this matches iba, it matchs " iba " (that is space iba space).
When it gets to the gtz part, it doesn’t match because it needs to
match a beginning of a line (\A) or a space (\s) again, but these
space before it has already been matched. So it fails.
To help in understanding this, add an extra space between the ibz and
gtz. Then it will work.