Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all alphanumeric words from a string in java

Tags:

regex

How to find a regular expression to remove all alphanumeric words from a string ?

Here is what i have tried unsuccessfully:

assume my string is: String mystring ="the food was thrill4 not2 that good 6son";

    mystring = mystring.replaceAll("[0-9A-Za-z]","");

but its not working.

The expected results should be: "the food was that good"

like image 1000
j2emanue Avatar asked Mar 04 '26 07:03

j2emanue


1 Answers

Try this:

\w*\d+\w*\s*

Demo

Details:

  • \w* - starting with 0 or more word chars
  • \d+ - one or more digit
  • \w* - ends with 0 or more word chars
  • \s* - match zero or more spaces after the word
like image 186
Gurwinder Singh Avatar answered Mar 07 '26 16:03

Gurwinder Singh



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!