RegEx는 kotlin에서 작동하지만 dart에서 예상대로 작동하지 않았습니다. [중복]

Dec 07 2020

정규식은 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())

출력 :

    [Today, ,,  scientists,  confirmed,  the,  worst,  possible,  outcome, :,  the,  massive,  asteroid,  will,  collide,  with,  Earth]

flutter와 동일한 정규식을 사용하려고했지만 예상대로 작동하지 않습니다.

다트 코드 :

    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}');

출력 :

[Today,,  scientists,  confirmed,  the,  worst,  , ossible,  outcome:,  the,  massive,  asteroid,  will,  collide,  with,  Earth]

답변

Iliya Dec 07 2020 at 02:53

문제는 기본적으로 정규식이 유니 코드 범주와 일치하지 않는다는 것입니다. unicode: true정규식이 이들과 일치 하려면을 추가해야 합니다. 시험:

    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}');
}

DartPad에서 작동합니다. 유니 코드는 일치 사용하지 않는 경우 p{L}p{N}문자 (PL) 및 PN한다.