Reputation: 62
For a dotnet application, I have a string pattern as shown below:
TEAM AND CO TEAM
TESTER
Name anderson rahul alice
and I need to only capture the value of "Name", but the value of name will be captured only when the string sequence will be "TEAM AND CO TEAM" is in one line and "TESTER" in next line and "Name" appearing on the next to next line.
Below is my regex but it is also selecting the second occurance of the Name, which I do not require :
\bName\s+(.*)$
input string :
TEAM AND CO TEAM
TESTER
Name anderson rahul alice
LTest SYNC DIM TESTER
PHYSICS
Name KIM SID andy
any help would be appreciable.
Upvotes: 2
Views: 68
Reputation: 163642
In your pattern \bName\s+(.*)$
you are only matching Name
and then capture the rest of the line after matching 1 or more whitespace characters.
You can extend the pattern to match the required line TEAM AND CO TEAM
and TESTER
in the next line.
Then match Name
and capture the value that you want in group 1.
Note to enable the multiline flag.
^TEAM AND CO TEAM\r?\nTESTER\r?\nName\s(.+)
Explanation
^
Start of stringTEAM AND CO TEAM\r?\n
Match TEAM AND CO TEAM
, an optional carriage return and a newlineTESTER\r?\n
Match TESTER
, an optional carriage return and a newlineName\s
Match Name
and a whitespace character(.+)
Capture group 1, match 1 or more chars which will match the rest of the lineSee a .NET regex demo
Upvotes: 1
Reputation: 18641
Use
(?m)(?<=^TEAM AND CO TEAM\r?\nTESTER\r?\nName\s).+
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
(?m) set flags for this block (with ^ and $
matching start and end of line) (case-
sensitive) (with . not matching \n)
(matching whitespace and # normally)
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
^ the beginning of a "line"
--------------------------------------------------------------------------------
TEAM AND CO TEAM 'TEAM AND CO TEAM'
--------------------------------------------------------------------------------
\r? '\r' (carriage return) (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
TESTER 'TESTER'
--------------------------------------------------------------------------------
\r? '\r' (carriage return) (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
Name 'Name'
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
.+ any character except \n (1 or more times
(matching the most amount possible))
Upvotes: 3