i want a python re to match exact one occurrence of / . indeed if occurs more / such as // or /// re shouldn't match any of them.
for example below inputs must convert:
a/b -> a b
a//b -> a//b
a/// -> a///b
You need (negative) lookarounds:
(?<!\/)\/(?!\/)
Demo
This regex matches a single forward slash, but only if it's not preceeded or followed by another slash.
You may use a regex with 2 capture groups:
r'^([^/]*)/([^/]*)$'
RegEx Demo
This regex:
^: Start([^/]*): matches 0 or more characters that are not / and captures in group #1/: Match a /([^/]*): matches 0 or more characters that are not / and captures in group #2$: EndCode:
import re
str = re.sub(r'^([^/]*)/([^/]*)$', r'\1 \2', str)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With