gpt4 book ai didi

c# - 需要 Regex 上的一个 block 是可选的

转载 作者:行者123 更新时间:2023-11-30 15:23:44 25 4
gpt4 key购买 nike

我有这个正则表达式 - "\^CV_(\w+)?-(\w+)?-Base"

var matches = new Regex(@"\^CV_(\w+)?-(\w+)?-Base").Matches("EnglUS^CV_Common-Concierge-Base^0^0^0");
var matchGroups = matches[0].Groups;
var parentCatMatch = matchGroups[1].Value;
var childCatMatch = matchGroups[2].Value;

我得到了上述值的正确正则表达式匹配和分组,但我得到了以下值的异常 -

**Exception:** Specified argument was out of the range of valid values. Parameter name: i 

var matches = new Regex(@"\^CV_(\w+)?-(\w+)?-Base").Matches("EnglUS^CV_Common-Base^0^0^0");
var matchGroups = matches[0].Groups;
var parentCatMatch = matchGroups[1].Value;
var childCatMatch = matchGroups[2].Value;

问题:我应该在正则表达式中更改什么,第二个单词 block - "Concierge"从这个单词 - "EnglUS^CV_Common-Concierge-Base^0^0^ 0

我在网上搜索了这个但没有成功,我对正则表达式的一些更改甚至完全停止了总匹配,所以如果对正则表达式更专业的人可以提供帮助,我会在这里发帖。

最佳答案

\^CV_(\w+)?(?:-(\w+))?-Base

^^

只需将 - 放在 optional group 中。查看演示。

https://regex101.com/r/hE4jH0/27

关于c# - 需要 Regex 上的一个 block 是可选的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34058774/

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