RegEx funciona com kotlin, mas não funcionou como esperado com dart [duplicado]
Dec 07 2020
O regex funciona bem no código kotlin:
var text = "Today, scientists confirmed the worst possible outcome: the massive asteroid will collide with Earth"
val encodeRegex = Regex("""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+""")
val x= encodeRegex.findAll(text).map { result ->
result.value
}
print(x.toList())
A saída:
[Today, ,, scientists, confirmed, the, worst, possible, outcome, :, the, massive, asteroid, will, collide, with, Earth]
Tentei usar o mesmo regexp com flutter, mas não funciona como esperado.
Código Dart:
final RegExp encodeRegex = RegExp(
r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+""",
);
final text ='Today, scientists confirmed the worst possible outcome: the massive asteroid will collide with Earth';
final tokens = encodeRegex
.allMatches(text)
.map(
(element) =>
element.group(0),
)
.toList();
print('${tokens}');
A saída:
[Today,, scientists, confirmed, the, worst, , ossible, outcome:, the, massive, asteroid, will, collide, with, Earth]
Respostas
Iliya Dec 07 2020 at 02:53
O problema é que, por padrão, a expressão regular não corresponde às categorias Unicode. Você precisa adicionar, unicode: true
para que a expressão regular corresponda a eles. Experimentar:
main(){
final RegExp encodeRegex = RegExp(
r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+""", unicode: true
);
final text ='Today, scientists confirmed the worst possible outcome: the massive asteroid will collide with Earth';
final tokens = encodeRegex
.allMatches(text)
.map(
(element) =>
element.group(0),
)
.toList();
print('${tokens}');
}
Funciona no DartPad. Se unicode não estiver ativado, ele corresponde p{L}
e p{N}
como pL e pN literais.
O que significa um erro “Não é possível encontrar o símbolo” ou “Não é possível resolver o símbolo”?