php - regex matches numbers, but not letters -


i have string looks this:

[if-abc] 12345 [if-def] 67890 [/if][/if]

i have following regex:

/\[if-([a-z0-9-]*)\]([^\[if]*?)\[\/if\]/s 

this matches inner brackets want to. however, when replace 67890 text (ie. abcdef), doesn't match it.

[if-abc] 12345 [if-def] abcdef [/if][/if]

i want able match characters, including line breaks, except opening bracket [if-.

this part doesn't work think does:

[^\[if] 

this match single character neither of [, i or f. regardless of combination. can mimic desired behavior using negative lookahead though:

~\[if-([a-z0-9-]*)\]((?:(?!\[/?if).)*)\[/if\]~s 

i've included closing tags in lookahead, avoid ungreedy repetition (which worse performance-wise). plus, i've changed delimiters, don't have escape slash in pattern.

so interesting part ((?:(?!\[/?if).)*) explained:

(         # capture contents of tag-pair   (?:     # start non-capturing group (the ?: performance           # optimization). group represents single "allowed" character     (?!   # negative lookahead - makes sure next character not mark           # start of either [if or [/if (the negative lookahead cause           # entire pattern fail if contents match)       \[/?if           # match [if or [/if     )     # end of lookahead     .     # consume/match single character   )*      # end of group - repeat 0 or more times )         # end of capturing group 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -