加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

需要C#Regex才能在句子中获得单词对

发布时间:2020-12-16 03:06:04 所属栏目:百科 来源:网络整理
导读:是否有正则表达式将采取以下句子: “我想把它分成两对” 并生成以下列表: “我想要”, “想要这个”, “这种分裂”, “分开”, “进入”, “成对” 解决方法 由于需要重复使用单词,因此需要先行断言: Regex regexObj = new Regex( @"( # Match and captur
是否有正则表达式将采取以下句子:

“我想把它分成两对”

并生成以下列表:

“我想要”,
“想要这个”,
“这种分裂”,
“分开”,
“进入”,
“成对”

解决方法

由于需要重复使用单词,因此需要先行断言:
Regex regexObj = new Regex(
    @"(     # Match and capture in backreference no. 1:
     w+    # one or more alphanumeric characters
     s+    # one or more whitespace characters.
    )       # End of capturing group 1.
    (?=     # Assert that there follows...
     (w+)  # another word; capture that into backref 2.
    )       # End of lookahead.",RegexOptions.IgnorePatternWhitespace);
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
    resultList.Add(matchResult.Groups[1].Value + matchResult.Groups[2].Value);
    matchResult = matchResult.NextMatch();
}

对于三人组:

Regex regexObj = new Regex(
    @"(     # Match and capture in backreference no. 1:
     w+    # one or more alphanumeric characters
     s+    # one or more whitespace characters.
    )       # End of capturing group 1.
    (?=     # Assert that there follows...
     (      # and capture...
      w+   # another word,s+   # whitespace,w+   # word.
     )      # End of capturing group 2.
    )       # End of lookahead.",RegexOptions.IgnorePatternWhitespace);

等等

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读