我是 RegEx 的新手。我想使用這個正則運算式從我的字串中取出所有音節:
/[^aeiouy]*[aeiouy] (?:[^aeiouy]*\$|[^aeiouy](?=[^aeiouy]))?/gi
我在 Dart 中這樣實作它:
void main() {
String test = 'hairspray';
final RegExp syllableRegex = RegExp("/[^aeiouy]*[aeiouy] (?:[^aeiouy]*\$|[^aeiouy](?=[^aeiouy]))?/gi");
print(test.split(syllableRegex));
}
問題:我得到串列中的單詞沒有被拆分。我需要更改什么才能將單詞劃分為串列。
我在 regex101 上測驗了 RegEx,它顯示為 Matches。但是當我在 Dart 中使用它時,firstMatch我得到了null
uj5u.com熱心網友回復:
你需要
- 在 Dart 中使用沒有正則運算式分隔符的純字串模式作為正則運算式模式
- 標志不能使用,
i被實作為一個caseSensitive選項,以RegExp和g實施為RegExp#allMatches方法 - 您需要匹配和提取,而不是與您的模式拆分。
您可以使用
String test = 'hairspray';
final RegExp syllableRegex = RegExp(r"[^aeiouy]*[aeiouy] (?:[^aeiouy]*\$|[^aeiouy](?=[^aeiouy]))?",
caseSensitive: true);
for (Match match in syllableRegex.allMatches(test)) {
print(match.group(0));
}
輸出:
hair
spray
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/420670.html
標籤:
