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

前端 未结 12 2637
旧时难觅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条回答
  •  孤城傲影
    2020-11-22 00:37

    Easy done:

    (?<=\[)(.*?)(?=\])
    

    Technically that's using lookaheads and lookbehinds. See Lookahead and Lookbehind Zero-Width Assertions. The pattern consists of:

    • is preceded by a [ that is not captured (lookbehind);
    • a non-greedy captured group. It's non-greedy to stop at the first ]; and
    • is followed by a ] that is not captured (lookahead).

    Alternatively you can just capture what's between the square brackets:

    \[(.*?)\]
    

    and return the first captured group instead of the entire match.

提交回复
热议问题