Replace captured groups with empty string in python

I currently have a line similar to the following:

str = 'abcHello Wor=A9ld'

What I want to do is find "abc" and "= A9" and replace these mapped groups with an empty string, so my last line is "Hello World".

I am currently using this regex that correctly finds the groups that I want to replace:

r'^(abc).*?(=[A-Z0-9]+)'

I tried replacing these groups using the following code:

clean_str = re.sub(r'^(abc).*?(=[A-Z0-9]+)', '', str)

Using the above code led to:

print(clean_str)
>>> 'ld'

My question is: how can I use re.sub to replace these groups with an empty string and get my "Hello World"?

+4
source share
4 answers

, ... , abc, ?

, , abc, , abc =[0-9A-Z]+.

:

import re
s="abcHello wo=A9rld"
if s.startswith('abc'):
    print(re.sub(r'=[A-Z0-9]+', '', s[3:]))

if s.startswith('abc'): , abc , s[3:] , abc, re.sub =[A-Z0-9]+.

, PyPi regex module, :

import regex
r = regex.compile(r'^abc|(?<=^abc.*?)=[A-Z0-9]+', regex.S)
print(r.sub('', 'abcHello Wor=A9ld=B56')) # Hello World
print(r.sub('', 'Hello Wor=A9ld'))        # => Hello Wor=A9ld

- Python

  • ^abc - abc
  • | -
  • (?<=^abc.*?) - , abc , , , .
  • =[A-Z0-9]+ - a =, ASCII.
+1

:

re.sub(r'^abc(.*?)=[A-Z0-9]+(.*)', r'\1\2', s)
+3

This is a naive approach, but why can't you use replacetwice instead of a regular expression, for example:

str = str.replace('abc','')
str = str.replace('=A9','')

print(str) #'Hello World'
+2
source

It worked for me.

re.sub(r'^(abc)(.*?)(=[A-Z0-9]+)(.*?)$', r"\2\4", str)
+2
source

Source: https://habr.com/ru/post/1680291/


All Articles