简体   繁体   English

正则表达式匹配字符串,但只能通过分组来带来一部分

[英]Regex to match for a string but only bringing a part of it by grouping

I have a string on the following form - for fiddle see here . 我有以下形式的字符串-小提琴请参见此处

{[(0.3;0.43)(10;8.2)(0;0)(0.7888;12.345)]:8.56/13.9} {[(0.3; 0.43)(10; 8.2)(0; 0)(0.7888; 12.345)]:8.56 / 13.9}

I would like to filter out just the coordinates in the parentheses. 我只想过滤出括号中的坐标。 So, the pattern to match I've set up recognized a pair of parentheses (two backslashes for escaping the grouping). 因此,我设置的匹配模式可以识别一对括号(两个反斜杠用于转义分组)。 Then, it creates a group that on its inside has the minimal set of characters as long as they're separated by a semicolon and enclosed in parentheses. 然后,它会创建一个在其内部具有最少字符集的组,只要它们之间用分号分隔并用括号括起来即可。 According to the debugger I have four matches, which suggest that it's correct. 根据调试器,我有四个匹配项,这表明它是正确的。 However, when I access Grouping , I see two elements - one containing a pair of brackets and one without. 但是,当我访问Grouping时 ,我看到两个元素-一个包含一对方括号,一个不包含方括号。

Regex regex = new Regex("\\((.*?;.*?)\\)");
string full = regex.Matches(figure.ToString())[0].Value;
string with = regex.Matches(figure.ToString())[0].Groups[0].Value;
string sans = regex.Matches(figure.ToString())[0].Groups[1].Value;

I'm not entirely sure where the first group ( Groups[0] ) gets its information from. 我不确定第一组( Groups [0] )从哪里获取信息。 I suspect that I haven't phrased the regular expression well enough and that it actually react to the escaped parentheses as if it was a grouping as well. 我怀疑我对正则表达式的措词还不够好,并且它实际上对转义的括号也有同样的反应。 Am I right in my suspicion? 我猜对了吗? How should I reformulate the expression? 我应该如何重新表达?

From https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.match.groups(v=vs.110).aspx : https://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.match.groups(v=vs.110).aspx

If the regular expression engine can find a match, the first element of the GroupCollection object (the element at index 0) returned by the Groups property contains a string that matches the entire regular expression pattern. 如果正则表达式引擎可以找到匹配项,则Groups属性返回的GroupCollection对象的第一个元素(索引为0的元素)将包含与整个正则表达式模式匹配的字符串。

So Groups[0] has the entire value you matched (eg (1;2) ), while Groups[1] is the first matched subgroup (eg 1;2 ). 因此, Groups[0]具有您匹配的整个值(例如(1;2) ),而Groups[1]是第一个匹配的子组(例如1;2 )。

Anything in parentheses is considered a group. 括号中的任何内容均视为一个组。 If you don't want the group to be considered in matches, you should prefix it with "?:" 如果您不想在比赛中考虑该组,则应在其前面加上“?:”

(?:REGEX) (?:REGEX)

will cause the group to be ignored as a result, but still matched against 结果将导致该组被忽略,但仍与

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM