Reputation: 115
I need to retrieve uri from string in a named capture group
Example uri's are,
/cu/rest/en_US/reports/execute/310F02501000016D001218A1AZ6B2D16
/cu/rest/en_US/reports/execute/newResource/
/cu/rest/en_US/link/xml
If the string contain 'execute' in it, regex should skip characters that appear after 'execute', otherwise it should match whole uri in given string
And the regex I am currently using is,
(?P<value>\S+)
this matches whole uri into 'value' even if contain 'execute' in it.
is it possible to have conditional regex based on which 'value' will be set to either contain whole uri or skip some part of it?
For 3 example above, 'value' should be
Upvotes: 1
Views: 1168
Reputation: 626699
You may use
^(?P<value>\S*?(?:/execute(?![^/])|$))
Or, with a tempered greedy token:
^(?P<value>(?:(?!/execute/)\S)*)
See the regex demo #1 and regex demo #2.
Details
^
- start of string(?P<value>\S*?(?:/execute(?![^/])|$))
- Group "value":
\S*?
- 0 or more non-whitespace chars, as few as possible (?:/execute(?![^/])|$)
- a non-capturing group matching
/execute(?![^/])
- a /execute
substring followed with /
or end of string (note that (?![^/])
= (?=/|$)
, but is slightly more efficient since contains no alternation and takes just 1 step)|
- or$
- end of string.The (?:(?!/execute/)\S)*
just matches any non-whitespace char (\S
), 0 or more occurrences (*
), that does not start the /execute/
char sequence. Note that in case you should ba all means check for a slash or end of string after /execute
, use /execute(?![^/])
instead of /execute/
.
Upvotes: 2