I am new in regular expression. I want to remove a uppercase letter if it has lowercase before and after it. If the input is "I wilYl go theXre"
then the output should be "I will go there"
. How can I get it?
Asked
Active
Viewed 337 times
-1

Ṃųỻịgǻňạcểơửṩ
- 2,517
- 8
- 22
- 33

Sajib
- 19
- 3
-
Regex for uppercase before lowercase is [a-z][A-Z] – Ṃųỻịgǻňạcểơửṩ Dec 09 '19 at 17:53
-
1"I will go thereX" returns what? – Dani Mesejo Dec 09 '19 at 17:53
-
`I wilYl go theXre` is input, `I will go there` is output. The `Y` and `X` are removed because it matches `[a-z][A-Z]` – Ṃųỻịgǻňạcểơửṩ Dec 09 '19 at 17:54
-
How can I write it in python? – Sajib Dec 09 '19 at 17:56
-
What does it means is ok? Do you want to remove it too? – Dani Mesejo Dec 09 '19 at 17:56
-
No only the uppercase which has lowercase before and after. Last uppercase is not necessary. – Sajib Dec 09 '19 at 18:04
1 Answers
1
You can use a lookaround:
import re
s='I wilYl go theXre'
print(re.sub(r'(?<=[a-z])([A-Z])(?=[a-z])','',s))
# ^ lookbehind ^lookahead
Prints:
I will go there

dawg
- 98,345
- 23
- 131
- 206