我在salesforce,apex的正则表达式方面遇到了麻烦。当我看到顶点使用与顶点相同的语法和逻辑时,我也将它针对java开发人员。
我调试了字符串,这是正确的。 street
等于'str 3 B'。
使用http://www.regexr.com/时,正则表达式有效('\ d \ w $')。
代码:
Matcher hasString = Pattern.compile('\\d \\w$').matcher(street);
if(hasString.matches())
我的问题是,hasString.matches()
解析为false
。任何人都可以告诉我,如果我做错了某事吗?我尝试在没有$
的情况下使用它,使用不同的外壳等等,我只是无法让它工作。
提前致谢!
答案 0 :(得分:2)
您需要使用find
代替matches
进行部分输入匹配,因为matches
会尝试匹配完整的输入文字。
Matcher hasString = Pattern.compile("\\d \\w$").matcher(street);
if(hasString.find()) {
// matched
System.out.println("Start position: " + hasString.start());
}