Maritn Ge
Maritn Ge

Reputation: 1235

How can I match around characters in python regex

Let's say I have a String like

abc{123}hij

And i want to remove all the digits inside of the curly braces. To match them, I would use

\{(\d+)\}, but i think looking from beginning to { and from } to end is easier.

(.+?\{)*(\}.+)* works, but it matches a few null matches. not a regex expert so i am not entirely sure what is happening here, but I think i have to exchange the * for something.

Thanks!

edit:

input: xxx{123}xxx

expected output/match: xxx{}xxx

Upvotes: 1

Views: 56

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627335

You can use

re.sub(r'{\d+}', '{}', text)

See the regex demo.

The {\d+} regex matches {, one or more digits, and then a } char. Since the curly braces are hard-coded in the pattern, it is easy to use them in the replacement pattern.

Here are some variations of the same solution:

re.sub(r'({)\d+(})', r'\1\2', text)
re.sub(r'(?<={)\d+(?=})', '', text)

Bonus: if you need to remove digits from between two curly braces, you can use

re.sub(r'{[^{}]*}', lambda x: ''.join([i for i in x.group() if not i.isdigit()]), text)

Here, {[^{}]*} matches any substrings between the curly braces and lambda x: ''.join([i for i in x.group() if not i.isdigit()]) removes all digits.

If you know the curly braces in your strings are paired and not nested, you can use

re.sub(r'\d+(?=[^{}]*})', '', text)

The \d+(?=[^{}]*}) regex matches one or more digits followed with zero or more chars other than { and } then followed with }.

Upvotes: 2

Related Questions