3

I'm working on MPXJ library. I want to get predecessors id from below string. It's complex for me. Please help me get all predecessors id. Thanks.

Task predecessor string:

Task Predecessors:[[Relation [Task id=12 uniqueID=145 name=Alibaba1] -> [Task id=10 uniqueID=143 name=Alibaba2]],
[Relation [Task id=12 uniqueID=145 name=Alibaba3] -> [Task id=11 uniqueID=144 name=Alibaba4]], [Relation [Task id=12 uniqueID=145 name=Alibaba5] -> [Task id=9 uniqueID=142 name=Alibaba6]]]

I need get the predecessors id: 10, 11, 9

Pattern:

[Task id=12 uniqueID=145 name=Alibaba1] -> [Task id=10 uniqueID=143 name=Alibaba2]]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

2 Answers2

3

No need for capture groups. See full C# online demo

My original answer used capture groups. But we don't need them.

You can use this regex:

(?<=-> \[Task id=)\d+

See the output of at the very bottom of this C# online demo:

10
11
9
  • The (?<=-> \[Task id=) lookbehind ensures that we are preceded by the section from the arrow to the equal sign
  • \d+ matches the id

This C# code adds all the codes to resultList:

var myRegex = new Regex(@"(?<=-> \[Task id=)\d+");
Match matchResult = myRegex.Match(s1);
while (matchResult.Success) {
resultList.Add(matchResult.Value);
Console.WriteLine(matchResult.Value);
matchResult = matchResult.NextMatch();
} 

Original Version with Capture Groups

To give you a second option, here is my original demo using a capture group.

Reference

zx81
  • 41,100
  • 9
  • 89
  • 105
3

To grab those ID's you need to look for the Task id after -> You can try the following using Matches method.

Regex rgx = new Regex(@"->\s*\[Task\s*id=(\d+)");

foreach (Match m in rgx.Matches(input))
        Console.WriteLine(m.Groups[1].Value);

Working Demo

Explanation:

->          # '->'
\s*         # whitespace (\n, \r, \t, \f, and " ") (0 or more times)
\[          # '['
 Task       # 'Task'
 \s*        # whitespace (\n, \r, \t, \f, and " ") (0 or more times)
 id=        # 'id='
  (         # group and capture to \1:
   \d+      #   digits (0-9) (1 or more times)
  )         # end of \1
hwnd
  • 69,796
  • 4
  • 95
  • 132