Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove digits but not when part of a word

Tags:

java

regex

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.

like image 896
Ankur Avatar asked Dec 05 '25 11:12

Ankur


2 Answers

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*","");
like image 182
Avinash Raj Avatar answered Dec 07 '25 01:12

Avinash Raj


\\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

like image 41
vks Avatar answered Dec 06 '25 23:12

vks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!