RegEx कोटलिन के साथ काम करता है लेकिन डार्ट के साथ अपेक्षित रूप से काम नहीं किया है [डुप्लिकेट]

Dec 07 2020

कोजलिन कोड में रेगेक्स ठीक काम करता है:

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]

मैं स्पंदन के साथ एक ही regexp का उपयोग करने की कोशिश की, लेकिन यह उम्मीद के मुताबिक काम नहीं करता है।

डार्ट कोड:

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

यह डार्टपैड में काम करता है। यदि यूनिकोड सक्षम नहीं है तो यह मेल खाता है p{L}और p{N}शाब्दिक pL और pN के रूप में।