[Tutor] problem in replacing regex

Kumar hihiren1 at gmail.com
Fri Apr 10 08:39:15 CEST 2009


Hey Moos/Denis/Kent,

Thanks a lot for your replies and suggestions.

Actually the value which I had given was separated by \n\r.
I now realized that this cannot be done with one regular expression as there
is one limit in negative look behind that it requires fixed width. I guess
This limit is there up to python 2.7 version.
So actually though I don't want, but I had to do it using splitting :(

But thanks a lot for your help, this has given me better understanding the
regex.
Thanks you all thank you so much.

Regards,
Kumar

On Thu, Apr 9, 2009 at 3:35 AM, Moos Heintzen <iwasroot at gmail.com> wrote:

> Hi,
>
> You can do the substitution in many ways.
>
> You can first search for bare account numbers and substitute them with
> urls. Then substitute urls into <a></a> tags.
>
> To substitute account numbers that aren't in urls, you simply
> substitutes account numbers if they don't start with a "/", as you
> have been trying to do.
>
> re.sub() can accept a function instead of a string. The function
> receives the match object and returns a replacement. This way you can
> do extra processing to matches.
>
> import re
>
> text = """https://hello.com/accid/12345-12
>
> 12345-12
>
> http://sadfsdf.com/asdf/asdf/asdf/12345-12
>
> start12345-12end
>
> this won't be replaced
> start/123-45end
> """
>
> def sub_num(m):
>        if m.group(1) == '/':
>                return m.group(0)
>        else:
>                # put url here
>                return m.group(1) + 'http://example.com/' + m.group(2)
>
> >>> print re.sub(r'(\D)(\d+-\d+)', sub_num , text)
> https://hello.com/accid/12345-12
>
> http://example.com/12345-12
>
> http://sadfsdf.com/asdf/asdf/asdf/12345-12
>
> starthttp://example.com/12345-12end
>
> this won't be replaced
> start/123-45end
>
> >>> _
>
> This is assuming there isn't any <a> tags in the input, so you should
> do this before substituting urls into <a> tags.
>
>
> I have super cow powers!
>
> Moos
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090410/973fb017/attachment-0001.htm>


More information about the Tutor mailing list