Find a complete word in a string java

前端 未结 9 2055
伪装坚强ぢ
伪装坚强ぢ 2021-01-16 06:52

I am writing a piece of code in which i have to find only complete words for example if i have

String str = \"today is tuesday\";

and I\'m

9条回答
  •  无人及你
    2021-01-16 07:12

    I would suggest using this regex pattern1 = ".\bt\b." instead of pattern2 = ".?\bt\b.?" . Pattern1 will help you to match the complete String if 't' occurs in that string rather than the pattern2 which just reaches the string "t" you are searching for and ignores rest of the string. There is not much difference in two approaches and for your particular use case of returning true/false will run fine both the ways. The one I suggested will help you to improvise the regex in case you make further changes in your use case

提交回复
热议问题