I have been struggling with my regex query for over 3 hours now. The issue is that I want to cut punctuation marks as below.
String: "th1s 1s a numer1c2w0rld. Say 'He11o W0r1d!'"
If I use regex expression [^"'\s-]\w*[\d]?\w*[^-!'"\s]
, it leaves out a
. Everything else is okay.
Here's another attempt [^"'\s-]\w*[_]{0,2}?\w*[^-!'."\s]
, but once again, one-letter words are ignored by regex. Please note that _
is optional, there could be max two underscores. Hence, I added the code for [_]{0,2}?
Can someone please help me? Thanks for your help.
I researched this topic on SO and found that most of the threads e.g. Regular expression to match a whole word or one letter these mostly deal with continuous words. My words are password-type. Meaning, they could have numeric data inside words. E.g. th1s
or even numer1cw0rld
.
Desired Output is the string of following words.
th1s
1s
a
numer1c2__w0rld
numeric_world
Say
trHe11o
W0r1d
Additional clarification: spaces are NOT allowed in the word. That's why I added \s
in my regex.
Additional clarification: The words cannot end or start with _
. However, "abcd_efgh" is valid.