user9486876
user9486876

Reputation:

python replace pattern with regex

I have this string.

a='SAD; Happy; ING:train coca'
OR
a='SAD; Happy; ING(train coca'
OR
a='SAD, Happy, ING[train coca'

I need to detect this string : "; ING:" for that I use this regex :

listRE=re.findall(r';\s*[A-Z]+\s*[\:|\[|\(]\s*[A-Z]+', a)

What i need to do is to delete what is between ; and : (not always ; : as shown in the regex)

I do that

for i in listRE:
   p=re.compile(i)
   a=re.sub(p, r'', a)

but it s deleting my text !! my target is :

a='SAD; Happy; train coca'

your help is more than welcome Thank you for your help

Upvotes: 0

Views: 218

Answers (3)

The fourth bird
The fourth bird

Reputation: 163217

If you also want to match the strings from the comments, you might use

\s+\w+\s?[:([]\s*

In the replacement use a space.

Regex demo | Python demo


If you can match either a colon or from an opening till closing bracket afterwards, you might use an alternation matching either the : or use 2 capturing group where you would match the content to keep between the brackets [...] and (...)

\s+\w+\s?(?::|\(\s*([^()]+)\s*\)|\[\s*([^]\[]+)\s*])\s*

In the replacement use a space and both capturing groups r' \1\2'

Regex demo | Python demo

Upvotes: 0

M Z
M Z

Reputation: 4799

You don't need to use findall – you can use a regex pattern directly that matches all cases you need. I've also fixed up some of your regex:

import re
a = 'SAD; Happy; ING:train coca'
b = "SAD; Happy; ING(train coca"
c = "SAD, Happy, ING[train coca"

print(re.sub(r'(?<=;|,)(\s*)[^:[(;,]*[:[(]', r'\1', a))
print(re.sub(r"(?<=;|,)(\s*)[^:[(;,]*[:[(]", r"\1", b))
print(re.sub(r"(?<=;|,)(\s*)[^:[(;,]*[:[(]", r"\1", c))

"""
output:
SAD; Happy; train coca
SAD; Happy; train coca
SAD, Happy, train coca
"""

Upvotes: 0

Toto
Toto

Reputation: 91385

This does the job:

import re

strs = [
    'SAD; Happy; ING:train coca',
    'SAD; Happy; ING(train coca',
    'SAD, Happy, ING[train coca',
]
for str in strs:
    x = re.sub(r'(?<=[;,])\s+[A-Z]+[:([]', ' ', str)
    print x

Output:

SAD; Happy; train coca
SAD; Happy; train coca
SAD, Happy, train coca

Demo & explanation

Upvotes: 1

Related Questions