我在 salesforce、apex 中的正则表达式遇到问题。当我看到 apex 使用与 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
。谁能告诉我我是否做错了什么?我尝试在没有 $
的情况下使用它,使用不同的大小写等,但我就是无法让它工作。
提前致谢!
最佳答案
您需要使用 find
而不是 matches
来进行部分输入匹配,因为 matches
尝试匹配完整的输入文本。
Matcher hasString = Pattern.compile("\\d \\w$").matcher(street);
if(hasString.find()) {
// matched
System.out.println("Start position: " + hasString.start());
}
关于Java(Apex) 正则表达式不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30126772/