[erlang-questions] dots in regexps
David Mercer
dmercer@REDACTED
Tue Mar 30 15:16:52 CEST 2010
On Tuesday, March 30, 2010, Fredrik Thulin wrote:
> This surprises me :
>
> 1> re:replace("123foobar456", ".*foo(...)", "foo\\1", [{return, list}]).
> "foobar456"
>
> I would have expected "foobar", because in my book a dot matches a
> single character.
>
> Adding .* after the parentheses gives me "foobar", but I don't think it
> should be necessary.
>
> 2> re:replace("123foobar456", ".*foo(...).*", "foo\\1",
> [{return, list}]).
> "foobar"
Parts of the string that do not match the regexp are unaffected by
re:replace, so are retained in the result.
Your regexp ".*foo(...)" is matching the "123foobar" in "123foobar456".
Replace it with whatever you like, the "456" will still be there. When you
replace "123foobar" with "foo\\1" = "foobar", you still have the "456"
tacked on. That is, "foobar" is replacing "123foobar", not the whole
string. When you add the ".*" to the end of your regexp, it now matches the
entire string, which is replaced with "foobar".
Cheers,
DBM
More information about the erlang-questions
mailing list