I want to delete from a string all numbers, except if they are stuck to a letter.
Ex : F5 45, AD12 should become: F5 , AD12
So far I have done that, which is not much... :
line.replaceAll("[0-9]+", ""))
If you have any idea... thx
I want to delete from a string all numbers, except if they are stuck to a letter.
Ex : F5 45, AD12 should become: F5 , AD12
So far I have done that, which is not much... :
line.replaceAll("[0-9]+", ""))
If you have any idea... thx
\b
is regexp-ese for 'word break'. You can look up the precise definition, but it'll probably do what you want here, because both spaces and 'start or end of string' all count as a word break, and the thing itself doesn't actually match characters (thus, it doesn't delete those spaces), it succeeds or fails based on whether there is a word-break character there.
Thus:
line.replaceAll("\\b[0-9]+\\b", "");
should get it done.
If I understand your use case correctly, it should be something like this:
line.replaceAll("([^a-zA-Z]+)[0-9]+", "$1")
The first bit matches "anything except the letters a-z", the rest matches numbers. The second parameter retains the first character matched, so we don't remove any spaces or commas from your input.