Home > database >  How to substitute different number of digits behind special character with regex
How to substitute different number of digits behind special character with regex

Time:02-14

I have the following kind of string:"§ 9,12,14,15 und 16" or "§ 9,12 und 16".

I want to change the string to:"§ 9, § 12, § 14, § 15 und §16" or "§ 9, § 12 und § 16", respectively.

The number of digits varies and I want a code snipped that is applicable for all length:

text = "§§ 9,12,14,15 und 16"
text = re.sub(r'§* (\d ),(\d ),(\d ),(\d ) und (\d )', r'§ \1, § \2, § \3, § \4 und § \5', text)

I only manage to match the string if I know the number of digits.

CodePudding user response:

There is no single regex which can do that. What you can do is split your string into parts, and perform a substitution on each.

text = "§§ 9,12,14,15 und 16"
parts = re.search(r'(§*)\s*((?:\d ,?\s*) )\s*und\s (\d )', text)
if parts:
    sections = parts.group(2)
    text = re.sub(r'(\d )', r'§\1', parts.group(2))   ' und §'   parts.group(3)

The spacing in your example ends up being a bit irregular but this can be fixed up with some light post-processing.

text = re.sub(r',(?!\s)', ', ', re.sub('\s ', ' ', text))
  • Related