String matches regexp [closed]

有些话、适合烂在心里 提交于 2021-02-17 05:39:07

问题


I used perl, unix and java regular expression lot of time, but I'm surprised in java about that:

    "help".matches("^h")  

is false!!

From java documentation: http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#matches-java.lang.String-)

"true if, and only if, this string matches the given regular expression"

     "help".matches("^h.*")

or

     "help".matches("^h.*$")

return of course true.

It's surprising only me?


回答1:


"help" does not macth "^h". Only the first letter in help matches "^h"




回答2:


Java is a bit more strict than say perl or ruby. It's trying to match the entire string and "help" has an extra elp at the end that /^h/ won't match on.

From the docs:

Tells whether or not this string matches the given regular expression.

Not a substring, the whole string.



来源:https://stackoverflow.com/questions/7418924/string-matches-regexp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!