Motheus
Motheus

Reputation: 543

How to replace and lowercase 2 or more groups using re in python

I'm trying to replace some uppercase bbcode tags to lowercase like this:

p=re.compile(r'\[URL="(.*?)"\](.*?)\[/URL\]',re.S+re.I)
message=p.sub('[url=\\1]\\2[/url]',message)

But i need to replace massive tags so I'm not doing a compile for each. If i use a [(.*?)] the sub will replace with same uppercased anyway.

So the question is:

How to replace and lowercase 2 or more groups using RE in Python

Upvotes: 2

Views: 1661

Answers (1)

Motheus
Motheus

Reputation: 543

Well then, i used the easy way for 1 group from here:

Using a regular expression to replace upper case repeated letters in python with a single lowercase letter

text='dsads [QUOTE]test[/QUOTE]<br><br>[URL=http://test.com]what[/URL] dsadkd [B]TEST[/B]'

def replacement(match):
  return "["+match.group(1).lower()+"]"

>>> re.sub(r'\[(.*?)\]', replacement, text)
RESULT: 'dsads [quote]test[/quote]<br><br>[url=http://test.com]what[/url] dsadkd [b]TEST[/b]'

Upvotes: 2

Related Questions