【问题标题】:How to determine syllables in a word by using regular expression如何使用正则表达式确定单词中的音节
【发布时间】:2020-07-16 22:14:51
【问题描述】:

鉴于我有一个故事。故事由文字组成。我需要构造一个正则表达式来计算故事中每个单词的音节数。

我尝试构造一个满足以下条件的正则表达式:

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'

预期输出:

计算每个单词的匹配结果应该是:

3 个音节表示 aerospace

1 音节 she

总共4个音节。

我能够构造 (?(?=([a-zA-Z]+e))(?=([aeiouy]))),但如果可能的话,我需要你的帮助才能在一个表达式中完成它。

【问题讨论】:

  • 为什么要匹配它们?您是在提取、计数、替换吗?你有一个aerospace she 字符串。预期的输出是什么?
  • @WiktorStribiżew 我编辑了我的问题。

标签: java regex


【解决方案1】:

在阅读了很多关于 Regex 和 Regex 条件的使用之后。 Java Regex 包默认不支持条件。 (在这里找到答案:Conditional Regular Expression in Java?

所以,最终构造了一个没有 if-else-then 条件的 Regex。

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

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

欢迎改进。

谢谢。

【讨论】:

    猜你喜欢
    • 2019-12-31
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 2013-02-11
    • 2020-04-20
    • 1970-01-01
    • 2021-11-17
    • 2013-03-17
    相关资源
    最近更新 更多