The Dodo
The Dodo

Reputation: 719

How to replace certain forward slashes differently with regex?

Lets say I have these strings:

this_string = 'US/Canada'

that_string = '/fowardslash @/t t/'

I want to be able to re.sub() the strings with these 2 goals:

1) replace all the / that does not have letters before and after it with nothing ''.

2) replace all the / that HAS a letter before and after it with a space.

So what I want to end up with would be something like this:

this_string = 'US Canada'

that_string = 'forwardslash @t t' 

I currently have this re.sub('[^A-Za-z0-9\s]+','', this_string)

which does the 1st goal but not the 2nd.

I would get this_string = 'USCanada'

Upvotes: 1

Views: 862

Answers (4)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You may use

import re
s = '''US/Canada
/fowardslash @/t t/'''
rx = r'(?<=[^\W\d_])/(?=[^\W\d_])|(/)'
print(re.sub(rx, lambda m: '' if m.group(1) else ' ', s))
# => US Canada
#    fowardslash @t t

See the Python 3 demo online.

The regex matches

  • (?<=[^\W\d_])/(?=[^\W\d_]) - a - surrounded with any Unicode letter
  • | - or
  • (/) - (Capturing group 1) a / char in any other context.

If Group 1 is not empty, if it matched, the match is removed, else, it is replaced with a space.

Upvotes: 0

oo-ar
oo-ar

Reputation: 1

Maybe the other way round?

text = re.sub(r'\b/\b' , ' ' , text) # Replace with space
text = re.sub(r'/'     , ''  , text) # Remove

Or:

text = re.sub(r'/', '', re.sub(r'\b/\b', ' ', text))

Upvotes: 0

Axifive
Axifive

Reputation: 1151

You can use re.sub() with own replace function.

Example:

import re

this_string = 'US/Canada'
that_string = '/fowardslash @/t t/'

def myreplace(match):
    if match.group(1) is not None and match.group(2) is not None:
        return match.group(1) + ' ' + match.group(2)
    else:
        return ''

print(re.sub(r'(?:([A-Za-z0-9]+)/([A-Za-z0-9]+))|(/)', myreplace, this_string))
print(re.sub(r'(?:([A-Za-z0-9]+)/([A-Za-z0-9]+))|(/)', myreplace, that_string))

Upvotes: 1

DarkSuniuM
DarkSuniuM

Reputation: 2582

You can use re.sub('\/', ' ', this_string) for second goal, \ will escape the / character and results to what you need.

But I don't think if it's possible to use same pattern for 2 different scenarios, you can use patterns together to achieve what you want

Upvotes: 1

Related Questions