1

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Freud
  • 15
  • 1
  • 3

2 Answers2

0

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]
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

Regex:

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;
}
Stas BZ
  • 1,184
  • 1
  • 17
  • 36