There doesn't seem to be a way to specify both RegexOptions and a start index when using Regex.Matches.
According to the docs , there is a way to do both individually, but not together.
In the example below, I want matches
to contain only the second hEllo
in the string text
string pattern = @"\bhello\b";
string text = "hello world. hEllo";
Regex r = new Regex(pattern);
MatchCollection matches;
// matches nothing
matches = r.Matches(text, 5)
// matches the first occurence
matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase)
Is there a different way to accomplish this?
I don't believe you can. You should instead instantiate Regex
using the desired options:
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
and then you can simply use your existing code from the first sample, which should now match since we're using the IgnoreCase
option:
matches = r.Matches(text, 5);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.