Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex expression to remove eed from string

Tags:

java

regex

I am trying to replace 'eed' and 'eedly' with 'ee' from words where there is a vowel before either term ('eed' or 'eedly') appears.

So for example, the word indeed would become indee because there is a vowel ('i') that happens before the 'eed'. On the other hand the word 'feed' would not change because there is no vowel before the suffix 'eed'.

I have this regex: (?i)([aeiou]([aeiou])*[e{2}][d]|[dly]\\b) You can see what is happening with this here.

As you can see, this is correctly identifying words that end with 'eed', but it is not correctly identifying 'eedly'.

Also, when it does the replace, it is replacing all words that end with 'eed' , even words like feed which it should not remove the eed

What should I be considering here in order to make it correctly identify the words based on the rules I specified?

like image 999
Anderology Avatar asked Feb 16 '16 19:02

Anderology


2 Answers

You can use:

str = str.replaceAll("(?i)\\b(\\w*?[aeiou]\\w*)eed(?:ly)?", "$1ee");

Updated RegEx Demo

\\b(\\w*?[aeiou]\\w*) before eed or eedly makes sure there is at least one vowel in the same word before this.

To expedite this regex you can use negated expression regex:

\\b([^\\Waeiou]*[aeiou]\\w*)eed(?:ly)?

RegEx Breakup:

\\b                 # word boundary
(                   # start captured group #`
   [^\\Waeiou]*     # match 0 or more of non-vowel and non-word characters
   [aeiou]          # match one vowel
   \\w*             # followed by 0 or more word characters
)                   # end captured group #`
eed                 # followed by literal "eed"
(?:                 # start non-capturing group
   ly               # match literal "ly"
)?                  # end non-capturing group, ? makes it optional

Replacement is:

"$1ee" which means back reference to captured group #1 followed by "ee"
like image 191
anubhava Avatar answered Sep 19 '22 20:09

anubhava


find dly before finding d. otherwise your regex evaluation stops after finding eed.

(?i)([aeiou]([aeiou])*[e{2}](dly|d))
like image 34
Ashraff Ali Wahab Avatar answered Sep 23 '22 20:09

Ashraff Ali Wahab