C#正则表达式匹配的例子 [英] c# regex matches example

查看:117
本文介绍了C#正则表达式匹配的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



输入

想获得使用下面的文本价值,有什么想法这可以用正则表达式完成:
的Lorem?存有悲坐%下载%#456阿梅德,consectetur adipiscing%下载%#3434 ELIT。 DUIS非NUNC NEC mauris feugiat porttitor。桑达tincidunt blandit DUI一个灵猫%下载%#298。 Aenean dapibus nisl%下载%#893434 ID NIBH auctor德维尔tempor velit blandit。



输出:结果
456结果
3434结果
298

事先893434



感谢。


解决方案

?所以,你试图抓住由令牌%下载%#开头的数值



试试这个模式:

 (小于?=%下载%#)\d + 

这应该工作。我不认为在.NET正则表达式的特殊字符,但你必须要么逃跑反斜线像 \\ 或使用的对整个模式逐字字符串

  VAR的regex =新的正则表达式(@ (小于?=%下载%#)\d +); 
返回regex.Matches(strInput);



在这里测试: HTTP: //rextester.com/BLYCC16700



注意:的向后断言(小于=? ..),因为你不希望包括下载%#%在搜索结果中,只有后的数字是很重要的。然而,你的例子似乎要捕获每个字符串前需要它。该集团后向将确保它的存在输入字符串,但不包括在返回的结果。 更多关于这里环视断言。


Am trying to get values using following text, any thoughts this can be done with Regex?

Input: Lorem ipsum dolor sit %download%#456 amet, consectetur adipiscing %download%#3434 elit. Duis non nunc nec mauris feugiat porttitor. Sed tincidunt blandit dui a viverra%download%#298. Aenean dapibus nisl %download%#893434 id nibh auctor vel tempor velit blandit.

Output:
456
3434
298
893434

Thanks in advance.

解决方案

So you're trying to grab numeric values that are preceded by the token "%download%#"?

Try this pattern:

(?<=%download%#)\d+

That should work. I don't think # or % are special characters in .NET Regex, but you'll have to either escape the backslash like \\ or use a verbatim string for the whole pattern:

var regex = new Regex(@"(?<=%download%#)\d+");
return regex.Matches(strInput);

Tested here: http://rextester.com/BLYCC16700

NOTE: The lookbehind assertion (?<=...) is important because you don't want to include %download%# in your results, only the numbers after it. However, your example appears to require it before each string you want to capture. The lookbehind group will make sure it's there in the input string, but won't include it in the returned results. More on lookaround assertions here.

这篇关于C#正则表达式匹配的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆