Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
i have a string with a single character and a digit, for eg J0. I have to write an expression to remove 0 and keep only that single character.
\d
Use a positive lookbehind assertion or capturing group to replace a digit which exists next to an uppercase letter with empty string.
name.replaceAll("(?<=[A-Z])[0-9]", "");
OR
name.replaceAll("([A-Z])[0-9]", "$1");
Add a comment
name.replaceAll("[0-9]$", "");
I tried in Python using regular expression With your assumption that you have a string with a single character and a digit, for eg J0
>>> import re >>> x = re.search('(\w)\d', 'j0') >>> x.groups() ('j',) >>> x.groups()[0] 'j'
You can use the pattern (\w)\d
(\w)\d
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
\dreplacement string : empty string.