La expresión regular funciona con kotlin pero no funcionó como se esperaba con dart [duplicado]
Dec 07 2020
La expresión regular funciona bien en el 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())
La salida:
[Today, ,, scientists, confirmed, the, worst, possible, outcome, :, the, massive, asteroid, will, collide, with, Earth]
Intenté usar la misma expresión regular con flutter pero no funciona como se esperaba.
Código de dardo:
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}');
La salida:
[Today,, scientists, confirmed, the, worst, , ossible, outcome:, the, massive, asteroid, will, collide, with, Earth]
Respuestas
Iliya Dec 07 2020 at 02:53
El problema es que, de forma predeterminada, la expresión regular no coincide con las categorías Unicode. Debe agregar, unicode: true
para que la expresión regular los coincida. Tratar:
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 en DartPad. Si unicode no está habilitado, coincide p{L}
y p{N}
como pL y pN literales.