Regular expression hangs - Java matcher

后端 未结 2 884
悲&欢浪女
悲&欢浪女 2021-01-23 09:20

String:

Aqua, Sodium Laureth Sulfate, Sodium Lauryl Sulfate, Dimethicone, Cocamide MEA, Zinc Carbonate, Glycol Distearate, Sodium Chloride, Zinc Pyrithion

2条回答
  •  迷失自我
    2021-01-23 09:48

    I recommend you split your input string by word and then pattern match it, event simpler: not to pattern match if you just want to test that the first letter of each word is uppercase, like:

    for (String s : string.split("\\W")) {
      if (s.charAt(0) < 'A' || s.charAt(0) > 'Z') {
        return false;
      }
    }
    

    Sounds a lot faster to me (and you can even have the word that failed if you need).

提交回复
热议问题