Java regex first match only

后端 未结 1 1013
执念已碎
执念已碎 2021-02-08 05:22

How do I tell the following regex to only find the FIRST match? The following code keeps finding all possible regex within the string.

i.e. I\'m looking only for the in

相关标签:
1条回答
  • 2021-02-08 05:43

    matcher.group(1) will return the first match.

    If you mean lazy matching instead of eager matching, try adding a ? after the + in the regular expression.

    Alternatively, you can consider using something more specific than .+ to match the content between the brackets. If you're only expecting letters, numbers and a few characters then maybe something like [-A-Z0-9;_.]+ would work better?

    0 讨论(0)
提交回复
热议问题