Force Close on Regular Expression !match

前端 未结 3 527
借酒劲吻你
借酒劲吻你 2021-01-05 15:15

I am having a problem with the following bit of code. I am trying to match a string. When I have a match everything works perfectly. When it does not find a match it thro

相关标签:
3条回答
  • 2021-01-05 15:35

    Try checking for a match instead:

    if (m.matches()){
    

    instead of:

    if (m.group(0) != ""){
    
    0 讨论(0)
  • 2021-01-05 15:37

    http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Matcher.html#find() the find method returns true if and only if a match is found. So before you call you have to ensure that matches are found.

    if (m.find()) {
        // do other stuff
    }
    
    0 讨论(0)
  • 2021-01-05 15:48

    This is behaving as documented. You are welcome to catch the exception rather than checking for the empty string.

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