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)
CodePudding user response:
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.
CodePudding user response:
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
CodePudding user response:
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]'
CodePudding user response:
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)