在 Dart 中仅获取带有正则表达式的货币符号
Get only the currency symbol with a regular expression in Dart
我正在尝试使用 正则表达式 :
从 Dart 中的 String
获取货币符号
void main() {
const String string = '€ 1,500.50';
final RegExp regExp = RegExp(r'([\p{Sc}])', unicode: true);
final Iterable<Match> matches = regExp.allMatches(string);
final onlyCurrency = StringBuffer();
for (final Match match in matches) {
onlyCurrency.write(match.group(0));
}
print(onlyCurrency);
}
但这不起作用。如何在 Dart 中使用正则表达式仅获取货币符号(无论它是什么)?
非常感谢!
您需要删除字符 class - 它是多余的 - 并使用 \p{Sc}
因为您是在原始字符串文字中定义正则表达式。
修复是
void main() {
const String string = '€ 1,500.50';
final RegExp regExp = RegExp(r'\p{Sc}', unicode: true);
final Iterable<Match> matches = regExp.allMatches(string);
final onlyCurrency = StringBuffer();
for (final Match match in matches) {
onlyCurrency.write(match.group(0));
}
print(onlyCurrency);
}
我正在尝试使用 正则表达式 :
从 Dart 中的String
获取货币符号
void main() {
const String string = '€ 1,500.50';
final RegExp regExp = RegExp(r'([\p{Sc}])', unicode: true);
final Iterable<Match> matches = regExp.allMatches(string);
final onlyCurrency = StringBuffer();
for (final Match match in matches) {
onlyCurrency.write(match.group(0));
}
print(onlyCurrency);
}
但这不起作用。如何在 Dart 中使用正则表达式仅获取货币符号(无论它是什么)?
非常感谢!
您需要删除字符 class - 它是多余的 - 并使用 \p{Sc}
因为您是在原始字符串文字中定义正则表达式。
修复是
void main() {
const String string = '€ 1,500.50';
final RegExp regExp = RegExp(r'\p{Sc}', unicode: true);
final Iterable<Match> matches = regExp.allMatches(string);
final onlyCurrency = StringBuffer();
for (final Match match in matches) {
onlyCurrency.write(match.group(0));
}
print(onlyCurrency);
}