user2063770
user2063770

Reputation: 57

Regex in c# that could either match new line or ignore it

Relatively new to Regular expressions in c#. Struggling to get this right.

Requirement:

Input string format : Some random text XXXX caused disturbance of XXXX hours of rework. Due to some random text reported in XXXX

I needed to fetch all the 3 XXXX from the input.

Concrete example: Complaint listed in some portal is like below RequirementChanges caused disturbance of 200 hours of rework. Due to this there is a request reported in Excel.

My regular expression should give the result : RequirementChanges , 200 hours, Excel.

The additional information about the input string is that : Only caused disturbance, of rework, reported in will always be there in the input string. Remaining could be any random text and line breaks could be anywhere in between except these 3 constant strings. I am planning to parse this input string in c#. Request your inputs. Thx,

Upvotes: 0

Views: 102

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174844

Use \s+ match all kind of vertical and horizontal line breaks. And it's opposite \S+ would match one or more non-space characters.

@"\S+(?=\s+caused disturbance)|\S+\s+\S+(?=\s+of rework)|(?<=\breported in\s+)\S+"

Code:

String input = @"Complaint listed in some portal is like below RequirementChanges caused disturbance of 200 hours of rework. Due to this there is a request reported in Excel.";
Regex rgx = new Regex(@"\S+(?=\s+caused disturbance)|\S+\s+\S+(?=\s+of rework)|(?<=\breported in\s+)\S+");
foreach (Match m in rgx.Matches(input))
Console.WriteLine(m.Groups[0].Value);

IDEONE

Upvotes: 1

Related Questions