Reputation: 3093
I have a string like this
IF condition EQUALS value THEN do this
I want to get the condition without any beginning or trailing spaces. This is what I tried:
/\b(IF)\b[\w\d\s]+?(?=EQUALS)(EQUALS)/g
That almost works except for the condition has whitespace before and after it.
See a full example here: https://regex101.com/r/qsjlia/4
Upvotes: 0
Views: 67
Reputation: 15000
In this proposed solution I've taken it a bit further by assuming that at some point you're going to want to have Condition, Equals Clause, Value, and both the Then & Else
This Regex
IF\s+((?:(?!\s(?:NOT)?EQUALS\s).)*)\s+((?:NOT)?EQUALS)\s+((?:(?!\sTHEN\s).)*)\s+THEN\s+((?:(?!\sELSE\s).)*)\s+ELSE\s+(.*)
** To see the image better, simply right click the image and select view in new window
Will do the following:
See also this live demo.
Given your sample text
IF 45 EQUALS 'Yes' THEN ASSIGN 28 ELSE SHOW 28
IF factor EQUALS value THEN do this ELSE do something else.
IF factor NOTEQUALS value THEN do this ELSE do something else.
IF 2 NOTEQUALS 'Droids' THEN Not the droids you are looking for ELSE Found the droids
The regex will create the following capture groups:
Condition
EQUALS
or NOTEQUALS
Value
Then
blockElse
blockWith the following Matches:
[0][0] = IF 45 EQUALS 'Yes' THEN ASSIGN 28 ELSE SHOW 28
[0][1] = 45
[0][2] = EQUALS
[0][3] = 'Yes'
[0][4] = ASSIGN 28
[0][5] = SHOW 28
[1][0] = IF factor EQUALS value THEN do this ELSE do something else.
[1][1] = factor
[1][2] = EQUALS
[1][3] = value
[1][4] = do this
[1][5] = do something else.
[2][0] = IF factor NOTEQUALS value THEN do this ELSE do something else.
[2][1] = factor
[2][2] = NOTEQUALS
[2][3] = value
[2][4] = do this
[2][5] = do something else.
[3][0] = IF 2 NOTEQUALS 'Droids' THEN Found the droids ELSE not the droids you are looking for
[3][1] = 2
[3][2] = NOTEQUALS
[3][3] = 'Droids'
[3][4] = Not the droids you are looking for
[3][5] = Found the droids
Each of the capture groups uses this an advanced Regex construct to capture until it encounters a known block of characters.
From the above expression one of those contructs is ((?:(?!\sTHEN\s).)*)\s+THEN\s+
NODE EXPLANATION
( group and capture
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
THEN 'THEN'
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character except \n
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
) end of capture group
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
THEN 'THEN'
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
NODE EXPLANATION
--------------------------------------------------------------------------------
IF 'IF'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount
possible)):
--------------------------------------------------------------------------------
NOT 'NOT'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
EQUALS 'EQUALS'
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
NOT 'NOT'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
EQUALS 'EQUALS'
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
THEN 'THEN'
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of \3
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
THEN 'THEN'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \4:
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
ELSE 'ELSE'
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of \4
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
ELSE 'ELSE'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \5:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of \5
Upvotes: 1