繁体   English   中英

正则表达式从字符串中删除eed

[英]regex expression to remove eed from string

我试图'eed' and 'eedly' with 'ee'替换'eed' and 'eedly' with 'ee'来自有元音的单词,然后出现任意一个词('eed' or 'eedly')

因此,例如,单词indeed会变为不indee因为有一个元音('i')发生在'eed'之前。 另一方面, 'feed'这个词不会改变,因为在后缀'eed'之前没有元音。

我有这个正则表达式:( (?i)([aeiou]([aeiou])*[e{2}][d]|[dly]\\\\b)你可以看到这里发生了什么。

正如您所看到的,这是正确识别以'eed'结尾的单词,但它没有正确识别'eedly'

此外,当它进行替换时,它将替换所有以'eed'结尾的单词,甚至是像feed这样的单词,它不应该删除eed

我应该在这里考虑什么,以便根据我指定的规则正确识别单词?

您可以使用:

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

更新了RegEx演示

\\\\b(\\\\w*?[aeiou]\\\\w*)eedeedly之前确保在此之前同一个单词中至少有一个元音。

加速这个正则表达式,你可以使用否定表达式正则表达式:

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

RegEx分手:

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

更换是:

"$1ee" which means back reference to captured group #1 followed by "ee"

在找到d之前找到dly。 否则你的正则表达式评估在找到eed后停止。

(?i)([aeiou]([aeiou])*[e{2}](dly|d))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM