Éireann__
Éireann__

Reputation: 113

substitution of unknown a priori number of groups - regex python

Given this string:

text = "hello world pattern 24 4 5 this is an example pattern 4 3 11 "

I need to substitute "pattern X Y Z" with "patternX-Y-Z", where X, Y, Z are numbers (no space between "pattern" and the first number). So far, I'm doing this through this regex :

text= re.sub('pattern\s(\d+)\s(\d+)\s(\d+)', r'pattern\1-\2-\3', text).strip()

Suppose I have more than three groups (something like "pattern 12 3 5 7 5 and pattern 34 5 4") where the number of groups is not fixed and it is unknown a priori, how could I write my regex? Is there a way for writing a recursive regex for substitution?

Upvotes: 3

Views: 184

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626728

You may use

import re
rx = r'(pattern)(\s*[\d\s]*\d)\b'
s = 'hello world pattern 24 4 5 this is an example pattern 4 3 11 6th oct 2018 pattern 4 3 11 124 2'
print(re.sub(rx, lambda x: "{}{}".format(x.group(1), "-".join(x.group(2).split())), s))
# => hello world pattern24-4-5 this is an example pattern4-3-11 6th oct 2018 pattern4-3-11-124-2

See the Python demo

The (pattern)(\s*[\d\s]*\d)\b matches

  • (pattern) - pattern into Group 1
  • (\s*[\d\s]*\d) - (Group 2) 0+ whitespaces, then 0+ digits and whitespaces and finally a digit
  • \b - a word boundary

When replacing, the Group 1 value is put at the beginning of the replacement, and Group 2 value is split with whitespace and joined back with -.

Upvotes: 2

Related Questions