Java comparing string to regex - while loop

后端 未结 2 1870
梦谈多话
梦谈多话 2021-01-21 05:48

I want the user to enter a string and if the string does not match my regex expression then I want a message to be outputted and the user to enter a value again.

The pro

相关标签:
2条回答
  • 2021-01-21 06:23

    The equals methods checks for string equivalence, not for matching regular expressions.

    Just replace equals with matches. You should also remove the outer square brackets.

    0 讨论(0)
  • 2021-01-21 06:25

    Could it be that your regexp is wrong?

    test.matches("[A-Z][a-zA-Z]*,\\s[A-Z][a-zA-Z]*")
    

    works fine for me.

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