Omega
Omega

Reputation: 871

Python regex wrap digits in underscores and replace all other underscores with dots

I would like to put all numbers in my strings into square brackets followed by a dot, which should essentially replace the underscores. All other underscores in the string should be replaced with dots.

What's the best way to go about this? I only need to remove the dot before [. 'fields' could be any string.

String:

fields_5_type_fields_1_name

Desired Output:

fields[5].type.fields[1].name

Current result:

fields.[5].type.fields.[1].name

Code:

i = re.sub(r"\d+", r"[\g<0>]", i)

i = re.sub(r"_", r".", i)

Upvotes: 1

Views: 871

Answers (4)

Mohammadreza
Mohammadreza

Reputation: 15

You've almost done everything, just change the first line:

i = re.sub(r"_(\d+)_", r"[\g<1>].", txt)

i = re.sub(r"_", r".", i)

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71461

You can apply re.sub twice:

import re
s = 'fields_5_type_fields_1_name'
new_s = re.sub('_\d+', lambda x:f'[{x.group()[1:]}]', re.sub('(?<=[a-z\d])_(?=[a-z])', '.', s))

Output:

'fields[5].type.fields[1].name'

This will also handle cases where an instance of index notation occurs without a subsequent .:

s = 'fields_5_type_fields_1'
print(re.sub('_\d+', lambda x:f'[{x.group()[1:]}]', re.sub('(?<=[a-z\d])_(?=[a-z])', '.', s)))

Output:

'fields[5].type.fields[1]'

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163477

You can use an alternation | to match either an optional _ followed by capturing 1+ digits in group 1, or match _ ad check for the group value to do the right replacement.

import re

pattern = r"_?(\d+)|_"
i = "fields_5_type_fields_1_name"
res = re.sub(pattern, lambda x: f"[{x.group(1)}]" if x.group(1) else ".", i)
print(res)

Output

fields[5].type.fields[1].name

Python demo

Upvotes: 2

Andrew
Andrew

Reputation: 982

i = re.sub(r"\d+", r"[\g<0>]", i)  # Put brackets around numbers.
i = re.sub(r"_\[", r"[", i)        # Remove the underscore before brackets.
i = re.sub(r"_", r".", i)          # Replace the remaining underscores.

Upvotes: 1

Related Questions