Regex not stopping at first space

后端 未结 5 1753
说谎
说谎 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:40

    I suggest using \[\S*(?=\s).

    • \[: Match a [ character.
    • \S*: Match 0 or more non-space characters.
    • (?=\s): Match a space character, but don't include it in the pattern. This feature is called a zero-width positive look-ahead assertion and makes sure you pattern only matches if it is followed by a space, so it won't match at the end of line.

    You might get away with \[\S*\s if you don't care about groups and want to include the final space, but you would have to clarify exactly which patterns need matching and which should not.

提交回复
热议问题