Regex / Preg: No match, if found

前端 未结 1 1288
不思量自难忘°
不思量自难忘° 2021-01-26 04:18

I\'m trying to do some PHP preg. But it seems to i can\'t get it to match if i want a string without something in it.

Example:

Hello! My name is [b]Peter         


        
相关标签:
1条回答
  • 2021-01-26 05:12

    It's unclear what you want to find. If it's just [b]Peter[/b], then you don't need a regex.

    If you want to find a single "word" surrounded by BBCode bold tags, use

    preg_match('%\[b\]\w*\[/b\]%', $subject)
    

    If you want to find anything within BBCode bold tags as long as it doesn't contain Jack, use

    preg_match(
        '%\[b\]     # Match [b]
        (?:         # Try to match...
         (?!Jack)   # (unless the word "Jack" occurs there)
         .          # any character
        )*?         # any number of times, as few as possible
        \[/b\]      # Match [/b]%x', 
        $subject)
    
    0 讨论(0)
提交回复
热议问题