4

我的测试字符串

“示例字符串 123 与示例字符串在海上有小词”

得到一个正则表达式:

var regex = new Regex(@"\b(?<tag>small|with)\b(?<param>.*)");

将结果分成两组:

G0=with, G1= 海面上有小字的示例字符串

G1 不好,我想看的是

G0=with, G1= 示例字符串 123

基本上我试图返回第一个匹配和第一个匹配之前的字符串。但是我在第一场比赛后得到了字符串

有人在我的 reg ex 中看到错误吗?#

4

2 回答 2

3

Reply to your Question: Regex, get the rest of the unmatched string

Simple

string toSearchString = "your string here";

Match match = new Regex("*some pattern here*").Match(toSearchString );

string unmatchedString = toSearchString.Replace(match.Value,"");

So now you have the Unmatched String. you can have coffee!!

Note: This answer is not relevant to the problem posted here but is relevant to the Question, This answer is for people who land up in this page seeking for fetching unmatched string.

于 2014-12-11T17:47:00.627 回答
2

试试这个模式:

(?<param>.*?)\b(?<tag>small|with)\b

这意味着交换捕获组的位置并使用 . 使第一组变得懒惰?

注意组的交换值。

查看实时示例

于 2013-06-12T13:51:37.500 回答