簡體   English   中英

正則表達式查找 Dart 中兩個字符串之間的所有字符串

[英]Regex to find all Strings enclosed between two Strings in Dart

我想在一個長字符串中找到所有出現的字符串,這些字符串位於一組兩個特定字符串之間。

例如。

String s = "abcdText('hello')abcd efghText('world')";

字符串的正則表達式模式將是Text('並且結果應該是包含在模式之間的字符串列表。因此,預期的 output 應該是:

[hello, world]

經過一番搜索,我找到了這個 這解釋了我的用例,但它在 PHP 中,僅用於查找數字。

你可以這樣試試:

  String myString = "abcdText('hello')abcd efghText('world')";
  RegExp exp = RegExp(r"\('(.*?)'\)");
  List<String> _list =[];
  for (var m in exp.allMatches(myString)) {
    _list.add(m[1].toString());
   
  }
  
  print(_list);

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM