我需要一种“缩写”句子的方法。我想从每个单词中至少取 3 个字母,并在元音或单词末尾完成每个单词的缩写。
例如,如果我有一个字符串“Profit Loss Report”,我想将其缩写为 ProfLossRep
有人可以推荐一个可以为我做这件事的正则表达式吗?
谢谢。
我需要一种“缩写”句子的方法。我想从每个单词中至少取 3 个字母,并在元音或单词末尾完成每个单词的缩写。
例如,如果我有一个字符串“Profit Loss Report”,我想将其缩写为 ProfLossRep
有人可以推荐一个可以为我做这件事的正则表达式吗?
谢谢。
我不确定您是否可以仅使用正则表达式替换来做到这一点。您绝对可以做的是编写一个可以做到这一点的小程序。这是 Ruby 中的一个简单的单行代码:
p 'Profit Loss Report'.split.map { |i| i[0, 4].gsub /[aeio]*$/, '' }.join
输出:
"ProfLossRep"
这是 .NET 的解决方案:
resultString = Regex.Replace(subjectString,
@"(?<= # Start at a position after...
\b # the start of a word
\p{L}{2,} # followed by at least two letters (any letters)
[^\P{L}aeiou] # and one letter that isn't a vowel
) # End of lookbehind
(?: # Then match...
[aeiou] # a vowel
\p{L}* # plus any additional letters
)? # if present.
\s* # Match any trailing whitespace",
"", RegexOptions.IgnorePatternWhitespace);
这个正则表达式似乎可以解决问题:
\b\w{3}[^aAeEyYuUiIoO\W$]*