Search code examples
c#regexregular-languagempxj

How to get predecessor id from task predecessor string?


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]]

Solution

  • 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