1

我有一个符合以下模式的字符串:

(cc)-(nr).(nr)M(nr)(cc)whitespace(nr)

其中cc是字母字符的任意数量,nr是任意数量的数字字符,并且M 是实际字母M

例如:

ASF-1.15M437979CA 100000
EU-12.15M121515PO 1145

我需要找到-,.M字符串的位置。问题是,前导字符和结尾字符也可以包含字母M,但我只需要中间的那个。

作为替代方案,减去第一个字符(直到-)和前两个数字(如(nr).(nr)M...)就足够了。

4

2 回答 2

0

如果您需要基于正则表达式的解决方案,您只需要围绕所需模式使用 3 个捕获组,然后访问该Groups[n].Index属性:

var rxt = new Regex(@"\p{L}*(-)\d+(\.)\d+(M)\d+\p{L}*\s*\d+");
// Collect matches
var matches = rxt.Matches(@"ASF-1.15M437979CA 100000  or  EU-12.15M121515PO 1145");
// Now, we can get the indices
var posOfHyphen = matches.Cast<Match>().Select(p => p.Groups[1].Index);
var posOfDot = matches.Cast<Match>().Select(p => p.Groups[2].Index);
var posOfM = matches.Cast<Match>().Select(p => p.Groups[3].Index);

输出:

posOfHyphen => [3, 32]
posOfDot    => [5, 35]
posOfM      => [8, 38]
于 2015-05-18T12:04:09.277 回答
0

正则表达式:

string pattern = @"[A-Z]+(-)\d+(\.)\d+(M)\d+[A-Z]+";
string value = "ASF-1.15M437979CA 100000  or  EU-12.15M121515PO 1145";

var match = Regex.Match(value, pattern);

if (match.Success)
{
    int sep1 = match.Groups[1].Index;
    int sep2 = match.Groups[2].Index;
    int sep3 = match.Groups[3].Index;
}
于 2015-05-18T12:05:13.117 回答