使用正则表达式来确定单词中的音节数。

huangapple go评论72阅读模式
英文:

How to determine syllables in a word by using regular expression

问题

给定我有一个故事。这个故事由单词组成。我需要构建一个正则表达式来计算故事中每个单词的音节数。

我试图构建一个正则表达式,满足以下条件:

如果单词以字符'e'结尾
并且单词中至少包含一个元音字符'a'|'e'|'i'|'o'|'u'|'y'
则不要匹配单词末尾的 'e'
但匹配单词中的所有其他元音
如果单词只包含一个独立的'e',并且在单词末尾
并且单词不包含其他元音字符
则匹配这个独立的 'e'

预期输出:

对于每个单词找到的匹配应该是:

对于 aerospace,有 3 个音节。

对于 she,有 1 个音节。

总共 4 个音节。

我能够构建出 (?(?=([a-zA-Z]+e))(?=([aeiouy]))),但需要你的帮助,如果可能的话,将其合并为单个表达式。

英文:

Given I have a story. The story consists of words. I need to construct a regular expression to count the number of syllables for each word in a story.

I try to construct a regular expression where the following is met:

IF word ends with character 'e'
AND word also contains at least one of the vowel characters 'a'|'e'|'i'|'o'|'u'|'y'
THEN do not match 'e' at the end of word
BUT match all the other vowels in word
IF word contains only a lone 'e' at the end of a word
AND word does not contain other vowel characters
THEN match the lone 'e'

Expected output:

Counting the matches found for each word should result in:

3 syllables for aerospace

1 syllable for she

A total of 4 syllables.

I was able to construct (?(?=([a-zA-Z]+e))(?=([aeiouy]))) but need some help from you to get it done in a single expression if that's possible.

答案1

得分: 0

在阅读了大量有关正则表达式以及正则表达式条件使用的资料之后,我发现 Java 正则表达式包默认不支持条件语句。可以在这里找到答案:https://stackoverflow.com/questions/3687921/conditional-regular-expression-in-java

因此,最终构建了一个不含 if-else-then 条件的正则表达式。

([aeiouyAEIOUY]+[^e.\s])|([aiouyAEIOUY]+\b)|(\b[^aeiouy0-9.']+e\b)

(https://regex101.com/r/gPO6mP/17)

欢迎提出改进意见。

谢谢。

英文:

After reading a lot about Regex and the use of Regex conditions. Conditionals are not supported by default by the Java Regex package. (Found the answer here: https://stackoverflow.com/questions/3687921/conditional-regular-expression-in-java)

So, finally constructed a Regex without if-else-then condition.

([aeiouyAEIOUY]+[^e.\s])|([aiouyAEIOUY]+\b)|(\b[^aeiouy0-9.']+e\b)

(https://regex101.com/r/gPO6mP/17)

Improvements are welcome.

Thanks.

huangapple
  • 本文由 发表于 2020年4月5日 03:55:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/61033977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定