I want to remove non-alphabet characters from an address, except when they followed by letters.
e.g. I want to remove 7 and e7 but not 7th
I think the english translation of the regex exp would be: remove all "words" that don't end with an alphabetic character?
I want to turn
850 7th Ave Frnt 1, New York, NY 10019
Into
7th Ave Frnt, New York, NY
Previously I was doing
address.replaceAll("\\d","");
But when I came up with words like 7th the 7 was disappearing, I want to keep these words entirely.
I've tried this (\d)$ but this seems to only match the final digit of 10019 (also tried many other variations) all seem far off the mark.
Use word boundaries.
address.replaceAll("\\b\\d+\\b","");
or
Add \s*, if you want to remove also the spaces which exists before the number.
address.replaceAll("\\s*\\b\\d+\\b\\s*","");
\\b\\d+\\b
Try this.See demo.
https://regex101.com/r/gT6vU5/7
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)
or using lookaheads.
(?:^|(?<=\W))\d+(?=\W|$)
https://regex101.com/r/gT6vU5/8
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