gpt4 book ai didi

.net - 如何按特定顺序从字符串中捕获一组组

转载 作者:行者123 更新时间:2023-12-02 15:09:11 24 4
gpt4 key购买 nike

我正在尝试构建一个替代数据条目,其中用户将表达某种命令,我将对其进行解析。这里没有详细介绍我将在这项工作中使用的词汇,而是通过一个例子来说明我试图通过对 Rex Harrison 的应用来实现的目标。

给出以下句子

the rain in spain falls on the plain

in spain on the plain falls the rain

on the meadow the snow falls in london

正则表达式中:

(the (?<weather>\w+)) (in (<?city>\w+)) (falls) (on the (?<topography>\w+))

简而言之,我需要使用正则表达式从句子中获取天气、城市和地形。

如何表达可以按任意顺序在输入中出现的一组捕获?

最佳答案

^(?:on the (?<area>\w+)() ?|the (?<weather>\w+)() ?|in (?<location>\w+)() ?|falls() ){4}\1\2\3\4$

将匹配包含每个元素的句子以任何顺序恰好匹配一次。这就是空括号的作用 - 每个人都必须参加比赛,所以最后的 \1\2\3\4可以匹配。

命名的反向引用将包含变量元素。

关于.net - 如何按特定顺序从字符串中捕获一组组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2237862/

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