Search code examples
c#regexstringqregexp

Regexp find position of different characters in string


I have a string conforming to the following pattern:

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

where cc is artbitrary number of letter characters, nr is arbitrary number of numerical characters, and M is is the actual letter M.

For example:

ASF-1.15M437979CA 100000
EU-12.15M121515PO 1145

I need to find the positions of -, . and M whithin the string. The problem is, the leading characters and the ending characters can contain the letter M as well, but I need only the one in the middle.

As an alternative, the subtraction of the first characters (until -) and the first two numbers (as in (nr).(nr)M...) would be enough.


Solution

  • If you need a regex-based solution, you just need to use 3 capturing groups around the required patterns, and then access the Groups[n].Index property:

    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);
    

    Output:

    posOfHyphen => [3, 32]
    posOfDot    => [5, 35]
    posOfM      => [8, 38]