Reputation: 1762
I'm fairly new to regex and trying to figure out a pattern that will only match the instance of the word inside my custom tag.
In the example below both words match the condition of being after a | and before a ]
Pattern: (?=|)singleline(?=.*])
Sample: [if @sample|singleline second] <p>Another statement singleline goes here</p> [/if]
Upvotes: 1
Views: 284
Reputation: 75222
(?=|)
asserts that the next thing in the string either nothing or nothing. That will always evaluate to true
; it's always possible to match nothing. I think sweaver2112 is correct that you meant to use a lookbehind there, but you also need to escape the pipe: (?<=\|)
. Or just match a pipe in the normal way; I don't see any need to use lookarounds for that part.
The other part probably does need to be a lookahead, but you need to expand it a bit. You want to assert that the word is followed by a closing bracket, but not if there's an opening bracket first. Assuming the brackets are always correctly paired, that should mean the word is between a pair of them. Like this:
Pattern: \|singleline(?=[^\]\[]*\])
[^\]\[]*\]
matches zero or more of any characters except ]
or [
, followed by a ]
. The backslashes escaping the "real" brackets may or may not be necessary depending on the regex flavor, but escaping them is always safe.
Upvotes: 1
Reputation: 7351
words that match the condition of being after a | and before a ]
the .*
, which means "anything, zero or more times, and be greedy about it", will race to the end of the string and back up only enough to get to a ]
(the last one). (and your lookbehind is a lookahead):
if you really want to match what you say you want to match (see quote), then this is it:
Pattern: (?<=|)(\w+)(?=])
Edit: or this one if you want to "match alphanumerics and spaces inside | and ]":
Pattern: (?<=|)([\w\s]+?)(?=])
Upvotes: 2