How to let regex ignore everything between brackets?

前端 未结 2 1157
渐次进展
渐次进展 2020-12-17 16:50

Consider the following string:

I have been driving to {Palm.!.Beach:100} and it . was . great!!

I use the following regex to delete all pun

相关标签:
2条回答
  • 2020-12-17 17:29
    $str = preg_replace('(\{[^}]+\}(*SKIP)(*FAIL)|[^a-zA-Z ]+)', '', $str);
    

    See also Split string by delimiter, but not if it is escaped.

    0 讨论(0)
  • 2020-12-17 17:35

    Try this

    [^a-zA-Z {}]+(?![^{]*})
    

    See it here on Regexr

    Means match anything that is not included in the negated character class, but only if there is no closing bracket ahead without a opening before, this is done by the negative lookahead (?![^{]*}).

    $string preg_replace('/[^a-zA-Z {}]+(?![^{]*})/', '', $string);
    
    0 讨论(0)
提交回复
热议问题