Regex not stopping at first space

后端 未结 5 1754
说谎
说谎 2021-01-05 03:20

Trying to create a pattern that matches an opening bracket and gets everything between it and the next space it encounters. I thought \\[.*\\s would achieve th

5条回答
  •  轮回少年
    2021-01-05 03:54

    \[[^\s]*\s
    

    The .* is a greedy, and will eat everything, including spaces, until the last whitespace character. If you replace it with \S* or [^\s]*, it will match only a chunk of zero or more characters other than whitespace.

    Masking the opening bracket might be needed. If you negate the \s with ^\s, the expression should eat everything except spaces, and then a space, which means up to the first space.

提交回复
热议问题