简体   繁体   English

正则表达式,查找匹配项

[英]Regular expressions, find matches

This regular expression gives answer 3 for count. 该正则表达式给出答案3。 How can I take only first and second "some" before "hello" ? 我怎样才能在“ hello”之前仅取第一和第二“ some”? Help me, please. 请帮帮我。

string SomeText ="Some some hello some" 
string patternSome = @"some";

RegexOptions RegOptions = RegexOptions.IgnoreCase |RegexOptions.CultureInvariant;
Regex newRegex = new Regex(patternSome, RegOptions );

MatchCollection matches = newRegex.Matches(SomeText);
Console.WriteLine("Count of matches {0}", matches.Count);

You can use lookahead regex: 您可以使用前瞻正则表达式:

\b[Ss]ome\b(?=.*hello)

RegEx Demo 正则演示

This will match some or Some only if it is followed by a hello . 仅当其后跟一个hello它才匹配someSome

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

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