[erlang-questions] dots in regexps

Dmitry Belayev <>
Tue Mar 30 16:40:07 CEST 2010


Ahh, sorry, I read the problem and looked at the second example.

Dmitry Belayev wrote:
>
> 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 (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
>
>



More information about the erlang-questions mailing list