簡體   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