My title probably doesn't explain exactly what I mean. Take the following string:
POWERSTART9^{{2|3}}POWERENDx{{3^EXSTARTxEXEND}}=POWERSTART27^{{1|4}}POWEREND
What I want to do here is isolate the parts that are like this:
{{2|3}} or {{1|4}}
The following expression works to an extent, it selects the first one {{2|3}} with no issue:
\{\{(.*?)\|(.*?)\}\}
The problem is, it's not just selecting the first if {{2|3}} and the second of {{1|4}} because after the first one we have {{3^EXSTARTxEXEND}} so it's taking the starting point from {{3 and going right until the end of the second part I want |4}}
Here it is highlighted on RegExr:
I've never been great with regex and can't work out how to stop it doing that. Any ideas? I basically want it to only match the exact pattern and not something that contains it.
You may use
\{\{((?:(?!{{).)*?)\|(.*?)}}
See the regex demo .
If there can be no {
and }
inside the {{...}}
substrings, you may use a simpler \\{\\{([^{}|]*)\\|([^{}]*)}}
expression (see demo ).
Details
\\{\\{
- a {{
substring ((?:(?!{{).)*?)
- Capturing group 1: any char ( .
), as few as possible ( *?
), that does not start a {{
char sequence ( tempered greedy token )
[^{}|]*
- any 0 or more chars other than {
, }
and |
\\|
- a |
char(.*?)
- Capturing group 2: any 0 or more chars, as few as possible
[^{}]*
- any 0 or more chars other than {
and }
}}
- a }}
substring. Try this \\{\\{([^\\^|]*)\\|([^\\^|]*)\\}\\}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.