3

I want to know if is possible to do this without checking for the regex pattern twice.

I am on python 3

pp = re.search(r'(.)(.+)\1(.+)', word)
word = re.sub(r'(.)(.+)\1(.+)', '', word)
salv = pp.groups()
word + = salv[0] + salv[0] + inverse(salv[1]) + salv[2]

There first I look for the matches, and then I remove the matches, but I am looking for the same regex patter twice. And I feel it can be done other way.

So what I want to do is:

Match a pattern, remove that pattern, and concat what I matched on a different way.

martineau
  • 119,623
  • 25
  • 170
  • 301
Azazel
  • 183
  • 1
  • 1
  • 10

2 Answers2

0

you could modify your regex pattern to return what you are looking for without the extra steps:

#  here we unpack the object, into the first group and the rest
#   |                  here we match anything else and add to first group
#   v                                  v
word_replacement, *slav = re.search(r'(.*)(.)(.+)\1(.+)', word)
# now everything is set up the same
word_replacement += slav[0] + slav[0] + inverse(slav[1]) + slav[2]

you can also use re.sub with \g<group> tags:

word_replacement = re.sub('(.)(.+)\1(.+)', '\g<1>\g<1>\g<2>\g<3>', word)

not sure how to implement inverse in the regex though.

alex elias
  • 321
  • 1
  • 8
0

You can use the re.sub method with a function as the value for its repl argument.

import re
word = 'mmabacbc'
print(re.sub(r'(.)(.+)\1(.+)', lambda m: m.group(1) * 2 + m.group(2).swapcase() + m.group(3), word))

Output:

mmaaBcbc

Test it online with Rextester.

Andrei Odegov
  • 2,925
  • 2
  • 15
  • 21