简体   繁体   English

如何在C#中的字符串的特定位置找到正则表达式匹配?

[英]How can I find a Regex match at a specific location of the string in C#?

I want to find out whether a Regex matches at a specific location of a string. 我想知道正则表达式是否匹配字符串的特定位置。

Example: 例:

Regex r = new Regex("d");
string s = "abcdefg";

I want the match function to find a match only if it is at the exact given location so that using the example above, matching at the locations 1 , 3 , and 5 should give no match , match , and no match , respectively. 我想匹配函数找到匹配, 只有当它使得使用上面的例子,在位置匹配是在精确的给定的位置13 ,及5应该给no matchmatchno match ,分别。 Unfortunately the C# Regex.Match method gives: 不幸的是,C# Regex.Match方法给出了:

r.Match(s, 1); // => match ("d")
r.Match(s, 3); // => match ("d")
r.Match(s, 5); // => no match

I understand this is because the Regex.Match method searches forward for the first match, but how do I prevent this behavior without having to make substrings? 我理解这是因为Regex.Match方法向前搜索第一个匹配,但是如何在不必创建子字符串的情况下阻止此行为?

Add \\G to the beginning of your regex: \\G添加到正则表达式的开头:

Regex r = new Regex(@"\Gd");
string s = "abcdefg";
Console.WriteLine(r.Match(s, 1).Success); // False
Console.WriteLine(r.Match(s, 3).Success); // True
Console.WriteLine(r.Match(s, 5).Success); // False

\\G anchors the match to the position where the previous match ended, or to the beginning of the string if there was no previous match. \\G将匹配锚定到上一个匹配结束的位置,如果没有先前的匹配,则将其锚定到字符串的开头。 With the second argument to Match , you're effectively telling it there was a previous match, which ended at that location. 随着第二个参数Match ,你有效地告诉它以前的比赛,这在该位置结束。

Use substring and the start-of-string anchor ^ : 使用substring和字符串开头的锚点^

Regex r = new Regex("^d"); // Use the start of string anchor
string s = "abcdefg";
r.IsMatch(s.Substring(3)); // Match at exactly fourth character (0-based index 3)

Alternatively, to avoid copying the string in memory, use quantified . 或者,为避免将字符串复制到内存中,请使用量化. :

Regex r = new Regex("^.{3}d");
r.IsMatch("abcdefg");

The pattern ^.{3}d says 模式^.{3}d

  • Start at the beginning of the string 从字符串的开头开始
  • Match exactly three characters of anything 恰好匹配任何东西的三个字符
  • Then match the letter 'd' 然后匹配字母'd'

Well, if you're always looking for the same index, you can stuff a little more your regex by adding wildcards at the beginning to "pad" the result, ie : 好吧,如果你总是寻找相同的索引,你可以通过在开头添加通配符来填充你的正则表达式,以“填充”结果,即:

Regex r = new Regex("^.{3}d");
r.isMatch("abcdefg"); // true
r.isMatch("adcffed"); // false
r.isMatch("abcddef"); // true

On the other hand, if you wanna use the same regex with different indexes, you can just use the ^ character to match the beginning of the string only : 另一方面,如果你想使用不同索引的相同正则表达式,你可以只使用^字符匹配字符串的开头:

Regex r = new Regex("^d");
r.isMatch("abcdefg".substring(3)); // true
r.isMatch("adcffed".substring(3)); // false
r.isMatch("abcddef".substring(1)); // false

NB : if you're just looking for a simple string and not a patter, you should simply use string.IndexOf 注意:如果你只是在寻找一个简单的字符串而不是一个模式,你应该只使用string.IndexOf

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

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