user2844265 user2844265 - 7 months ago 13
Ruby Question

How gsub works in ruby with meta characters in input

I need help in understanding how the following works.

"middl'-.*$%ddlemiddlemiddlemiddlemiddlemiddlemiExcess".gsub(/[^a-zA-Z'-.]/, '')
# => "middl'-.*ddlemiddlemiddlemiddlemiddlemiddlemiExcess"

"middl'-.*$%ddlemiddlemiddlemiddlemiddlemiddlemiExcess".gsub(/[^a-zA-Z.'-]/, '')
# => "middl'-.ddlemiddlemiddlemiddlemiddlemiddlemiExcess"


When I give
/[^a-zA-Z'-.]/
, then the star is not removed, but in the second example, the star is removed. Why?

I want the result after
gsub
to have only letters (
a-zA-Z
), period (
.
), hypen (
-
), single apostphe (
'
) to exist. Just by changing the period position inside regular expression the output is different ?

Answer

In /[^a-zA-Z'-.]/ hyphen is treated as range delimiter, exactly as in A-Z before. The range is:

▶ ("'"..'.').to_a
#⇒ ["'", "(", ")", "*", "+", ",", "-", "."] # note asterisk

In /[^a-zA-Z.'-]/ hyphen is the last symbol and hence it is treated as hyphen itself.

Comments