简体   繁体   English

正则表达式在rubular中匹配,而不是在java中

[英]regex matches in rubular, not in java

Im having problems getting some regex expressions to work using java.util.regex's pattern matchers. 我在使用java.util.regex的模式匹配器获取一些正则表达式时遇到了问题。 I have the following expression: 我有以下表达式:

(?=^.{1,6}$)(?=^\d{1,5}(,\d{1,3})?$)

i test matches against the following strings: 我测试匹配以下字符串:

12345  (match OK)
123456 (no match)
123,12 (match OK)

When i test it out on the following sites it seems to work perfectly: 当我在以下网站上测试它似乎完美地工作:

http://rubular.com , ok http://rubular.com ,好的

http://www.regextester.com/ , ok http://www.regextester.com/ ,好的

http://myregextester.com/index.php , ok http://myregextester.com/index.php ,好的

However i cant seem to get it to match anything in my java program. 但是我似乎无法匹配我的java程序中的任何内容。 Also, an online java regex tester gives the same result (no matches) : 此外,在线java正则表达式测试程序给出相同的结果(没有匹配):

http://www.regexplanet.com/advanced/java/index.html No matches ??? http://www.regexplanet.com/advanced/java/index.html没有匹配???

I don't have a clue why i can't get this to work in java, but seems to work in a lot of other regex engines? 我不知道为什么我不能在java中使用它,但似乎在许多其他正则表达式引擎中工作?

Edit: this was the non-working code. 编辑:这是非工作代码。 Excuse typo's , i cant copy/paste from my code-pc to stackoverflow. 借口拼写错误,我无法从我的代码pc复制/粘贴到stackoverflow。

String inputStr = "12345";
String pattern = "(?=^.{1,6}$)(?=^\\d{1,5}(,\\d{1,3})?$)";
Pattern regexp = Pattern.compile(pattern);
System.out.println("Matches? "+regexp.matcher(inputStr).matches());
System.out.println(inputStr.matches(pattern));

First of all you need to escape the \\ s in the pattern. 首先,您需要在模式中转义\\ s。 Then if you use matches() , Java tries to match against the entire string, so it will return false unless you either remove the second lookahead or add a .* at the end. 然后,如果使用matches() ,Java会尝试匹配整个字符串,因此除非您删除第二个前瞻或在末尾添加.* ,否则它将返回false。

This produces the right output in Java: 这在Java中产生了正确的输出:

    String regex = "(?=^.{1,6}$)^\\d{1,5}(,\\d{1,3})?$";
    System.out.println("12345".matches(regex)); 
    System.out.println("123456".matches(regex)); 
    System.out.println("123,12".matches(regex));

And so does this expression: 这个表达式也是如此:

    String regex = "(?=^.{1,6}$)(?=^\\d{1,5}(,\\d{1,3})?$).*";

The difference between the tools is, that in one case it tries to find a match in the other it tries to match the whole string. 这些工具之间的区别在于,在一种情况下,它试图在另一种情况下找到匹配,它试图匹配整个字符串。 If you use string.matches(regex) in java, this will return false for all of your inputs as you do not match the whole string with your lookahead expression. 如果在java中使用string.matches(regex),则对于所有输入都将返回false,因为整个字符串与前瞻表达式不匹配。 Either you append a .* as Keppil suggests, or you use the Matcher class: 您可以附加.*作为Keppil建议,或者您使用Matcher类:

Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(text);
if(matcher.find()) {
    System.out.println("Match found");
}

It's working correctly. 它工作正常。 You're probably using the matches() method, which expects the regex to match and consume the whole string. 您可能正在使用matches()方法,该方法期望正则表达式匹配并使用整个字符串。 Your regex doesn't consume anything because it's just a couple of lookaheads. 你的正则表达式不消耗任何东西,因为它只是几个前瞻。 On the RegexPlanet site, look at the find() column and you'll see the results you expect. 在RegexPlanet站点上,查看find()列,您将看到您期望的结果。 In your Java code, you have to create a Matcher object so you can use its find() method. 在Java代码中,您必须创建一个Matcher对象,以便可以使用其find()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM