Java regex capture not working

后端 未结 4 1158
逝去的感伤
逝去的感伤 2020-12-22 11:49

I have a regular expression:

l:([0-9]+)

This should match this string and return three captures (according to Rubular)

\"l:         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-22 12:41

    Calling Matcher.find finds the next instance of the match, or returns false if there are no more. Try calling it three times and see if you have all your expected groups.

    To clarify, m.group(1) is trying to find the first group expression in your regular expression. You only have one such group expression in your regex, so group(2) would never make sense. You probably need to call m.find() in a loop until it returns false, grabbing the group result at each iteration.

提交回复
热议问题