Locane
Locane

Reputation: 3134

re.sub() - Replace with text from match without using capture groups?

I'm trying to figure out how I can make python's re module behave more like grep in colored output.

This means that I need to take an abritrary regex that I have no control over, find it in a string, and then colorize just the match. Because I have no control over the regex, I can't rely on capture groups for my solution.

For example, if I have:

s="omglolwtfbbq"
regex=r"l[\w]"

I want python to replace lo and lw with a string that uses the match in it, without capture groups in the regex. So something like:

re.sub(regex, "!%s!", s)
re.sub(regex, "!\0!", s)
re.sub(regex, "!<THE MATCHING STRING>!", s)

Would produce:

"omg!lo!!lw!tfbbq"

Ultimately, I'm simply trying to colorize the portion of s that matches the sent regex; grep does this with a simple string input and no capture groups.

Is this possible in python?

Upvotes: 0

Views: 331

Answers (2)

Andrew
Andrew

Reputation: 151

To answer your first question, re.sub allows you to use a function instead of a fixed replacement string. E.g.

>>> s = "omglolwtfbbq"
>>> regex = r"l[\w]"
>>> re.sub(regex, lambda x: "!%s!" % x.group(), s)
'omg!lo!!lw!tfbbq'

Note that the .group method of a match object returns the whole match (whether or not capture groups are present). If you have capture groups, then .groups returns those captured groups.

To answer your question about colouring specifically, I would recommend taking a look at colorama.

Upvotes: 1

Emma
Emma

Reputation: 27723

I don't think Python re module does that, instead of which, you can design positive look-arounds to do such re.sub, for instance:

import re

print(re.sub(r'(?=l)', '!', "omglolwtfbbq"))

for placing a ! right before l

Output

omg!lo!lwtfbbq

The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.

Upvotes: 0

Related Questions