Regular Expression to find a string included between two characters while EXCLUDING the delimiters

前端 未结 12 2474
旧时难觅i
旧时难觅i 2020-11-21 23:49

I need to extract from a string a set of characters which are included between two delimiters, without returning the delimiters themselves.

A simple example should b

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 00:27

    [^\[] Match any character that is not [.

    + Match 1 or more of the anything that is not [. Creates groups of these matches.

    (?=\]) Positive lookahead ]. Matches a group ending with ] without including it in the result.

    Done.

    [^\[]+(?=\])
    

    Proof.

    http://regexr.com/3gobr

    Similar to the solution proposed by null. But the additional \] is not required. As an additional note, it appears \ is not required to escape the [ after the ^. For readability, I would leave it in.

    Does not work in the situation in which the delimiters are identical. "more or less" for example.

提交回复
热议问题