Java Regex:如何匹配一个或多个空格字符 [英] Java Regex : How to match one or more space characters

查看:692
本文介绍了Java Regex:如何匹配一个或多个空格字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java正则表达式中匹配多个空格字符?

How do you match more than one space character in Java regex?

我有一个正在尝试匹配的正则表达式。当我有两个或更多空格字符时,正则表达式失败。

I have a regex I am trying to match. The regex fails when I have two or more space characters.

public static void main(String[] args) { 
    String pattern = "\\b(fruit)\\s+([^a]+\\w+)\\b"; //Match 'fruit' not followed by a word that begins with 'a'
    String str = "fruit apple"; //One space character will not be matched
    String str_fail = "fruit  apple"; //Two space characters will be matched
    System.out.println(preg_match(pattern,str)); //False (Thats what I want)
    System.out.println(preg_match(pattern,str_fail)); //True (Regex fail)
}

public static boolean preg_match(String pattern,String subject) {
    Pattern regex = Pattern.compile(pattern);
    Matcher regexMatcher = regex.matcher(subject);
    return regexMatcher.find();
}


推荐答案

问题实际上是因为回溯。你的正则表达式:

The problem is actually because of backtracking. Your regex:

 "\\b(fruit)\\s+([^a]+\\w+)\\b"

说水果,后跟一个或多个空格,其次是一个或多个非'a'字符,后跟一个或多个'字'字符。这有两个空格失败的原因是因为 \s + 匹配第一个空格,但返回第二个,然后满足 [^ a] + (带第二个空格)和 \s + 部分(第一个)。

Says "fruit, followed by one or more spaces, followed by one or more non 'a' characters, followed by one or more 'word' characters". The reason this fails with two spaces is because \s+ matches the first space, but then gives back the second, which then satisfies the [^a]+ (with the second space) and the \s+ portion (with the first).

我认为你可以通过简单地使用posessive量词来修复它,它将是 \s ++ 。这告诉 \s 以返回第二个空格字符。您可以在此处找到有关Java量词的文档。

I think you can fix it by simply using the posessive quantifier instead, which would be \s++. This tells the \s not to give back the second space character. You can find the documentation on Java's quantifiers here.

作为示例,以下是Rubular的两个示例:

As an illustration, here are two examples at Rubular:


  1. \s 上使用占有量词(根据您的描述给出预期结果)

  2. 您当前的正则表达式与 [^ a\] + \w + 周围的单独分组。请注意,第二个匹配组(表示 [^ a] + )正在捕获第二个空格字符。

  1. Using the possessive quantifier on \s (gives expected results, from what you describe)
  2. Your current regex with separate groupings around [^a\]+ and \w+. Notice that the second match group (representing the [^a]+) is capturing a the second space character.

这篇关于Java Regex:如何匹配一个或多个空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆