gpt4 book ai didi

regex - 将所有特殊字符和单词分成字符串列表中的项目-Regex

转载 作者:行者123 更新时间:2023-12-03 03:09:31 26 4
gpt4 key购买 nike

我试图将一个字符串拆分为一个字符串列表,单词是分开的,但是周围的字符,例如.. "?()“”!"也分开。

要分隔的字符串是"testing “testing” “one two three” (hi there.) !word"
我想要的输出是

[",testing,",testing,",",one,two,three,",(,hi,there,.,),!,word]

我一直在使用以下几乎可以使用的Regex,但是它似乎并没有像(``等。
RegExp regex = RegExp("(?=[,.?!“”()])|\\s+");


list = context.split(regex).toList();

来自Regex大师的任何建议或帮助将不胜感激。

最佳答案

除了拆分外,您可以匹配一个或多个单词字符,也可以匹配除单词或空格字符之外的任何字符,以获取单独的周围字符。

[,.?!“”()]|[^,.?!“”()\s]+

说明
  • [,.?!“”()]匹配任何列出的
  • |
  • [^,.?!“”()\s]+匹配相反的字符,除了空格字符

  • Regex demo | Dart demo

    范例程式码
    void main() {
    final _regExp = RegExp(r'[,.?!“”()]|[^,.?!“”()\s]+');
    Iterable<String> matches = _regExp.allMatches("testing “testing” “one two three” (hi there.) !word").map((m)=>m[0]);
    print(matches);
    }

    输出量
    (testing, “, testing, ”, “, one, two, three, ”, (, hi, there, ., ), !, word)

    关于regex - 将所有特殊字符和单词分成字符串列表中的项目-Regex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61452820/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com