[erlang-questions] dots in regexps
Dmitry Belayev
rumata-estor@REDACTED
Tue Mar 30 16:35:26 CEST 2010
Then why first .* is greedy and last one is not?
David Mercer wrote:
> 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
>
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>
>
More information about the erlang-questions
mailing list