Java 如何在正则表达式中匹配“任何字符”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2912894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:25:38  来源:igfitidea点击:

How to match "any character" in regular expression?

javaregex

提问by Saobi

The following should be matched:

应符合以下条件:

AAA123
ABCDEFGH123
XXXX123

can I do: ".*123"?

我能做:".*123"

采纳答案by Delan Azabani

Yes, you can. That should work.

是的你可以。那应该工作。

  • .= any char
  • \.= the actual dot character
  • .?= .{0,1}= match any char zero or one times
  • .*= .{0,}= match any char zero or more times
  • .+= .{1,}= match any char one or more times
  • .= 任何字符
  • \.= 实际的点字符
  • .?==.{0,1}匹配任何字符零次或一次
  • .*==.{0,}匹配任何字符零次或多次
  • .+==.{1,}匹配任何字符一次或多次

回答by thr

Use the pattern .to match any character once, .*to match any character zero or more times, .+to match any character one or more times.

使用模式.匹配任意字符一次,.*匹配任意字符零次或多次, .+匹配任意字符一次或多次。

回答by Huusom

No, *will match zero-or-more characters. You should use +, which matches one-or-more instead.

不,*将匹配零个或多个字符。您应该使用+, 它匹配一个或多个。

This expression might work better for you: [A-Z]+123

这个表达式可能更适合你: [A-Z]+123

回答by polygenelubricants

There are lots of sophisticated regex testing and development tools, but if you just want a simple test harness in Java, here's one for you to play with:

有许多复杂的正则表达式测试和开发工具,但如果您只想要一个简单的 Java 测试工具,这里有一个供您使用:

    String[] tests = {
        "AAA123",
        "ABCDEFGH123",
        "XXXX123",
        "XYZ123ABC",
        "123123",
        "X123",
        "123",
    };
    for (String test : tests) {
        System.out.println(test + " " +test.matches(".+123"));
    }

Now you can easily add new testcases and try new patterns. Have fun exploring regex.

现在您可以轻松添加新测试用例并尝试新模式。玩得开心探索正则表达式。

See also

也可以看看

回答by BlueRaja - Danny Pflughoeft

Yes that will work, though note that .will not match newlines unless you pass the DOTALLflag when compiling the expression:

是的,这会起作用,但请注意,.除非在编译表达式时传递DOTALL标志,否则不会匹配换行符:

Pattern pattern = Pattern.compile(".*123", Pattern.DOTALL);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.matches();

回答by Ravi Shekhar

Try the regex .{3,}. This will match all characters except a new line.

试试正则表达式.{3,}。这将匹配除新行之外的所有字符。

回答by Akash Kumar Seth

Specific Solution to the example problem:-

示例问题的具体解决方案:-

Try [A-Z]*123$will match 123, AAA123, ASDFRRF123. In case you need at least a character before 123use [A-Z]+123$.

尝试[A-Z]*123$将匹配123, AAA123, ASDFRRF123。如果您在123使用前至少需要一个字符[A-Z]+123$

General Solution to the question (How to match "any character" in the regular expression):

问题的一般解决方案(如何匹配正则表达式中的“任何字符”):

  1. If you are looking for anything including whitespace you can try [\w|\W]{min_char_to_match,}.
  2. If you are trying to match anything except whitespace you can try [\S]{min_char_to_match,}.
  1. 如果您正在寻找包括空格在内的任何内容,您可以尝试[\w|\W]{min_char_to_match,}.
  2. 如果您尝试匹配除空格以外的任何内容,您可以尝试[\S]{min_char_to_match,}.

回答by Abrahan Gonzalez

I work this Not always dot is means any char. Exception when single line mode. \p{all}should be

我的工作并不总是点是任何字符。单行模式时例外。\p{all}应该

String value = "|°?<>!\"#$%&/()=?'\??/*-+_@[]^^{}";
String expression = "[a-zA-Z0-9\p{all}]{0,50}";
if(value.matches(expression)){
    System.out.println("true");
} else {
    System.out.println("false");
}

回答by Jamie Davis

The most common way I have seen to encode this is with a character class whose members form a partition of the set of all possible characters.

我见过的最常见的编码方式是使用字符类,其成员构成所有可能字符集的一个分区。

Usually people write that as [\s\S](whitespace or non-whitespace), though [\w\W], [\d\D], etc. would all work.

通常人们写为[\s\S](空格或无空格),但是[\w\W][\d\D]等一切都能解决。

回答by Anonymous

[^]should match any character, including newline. [^CHARS]matches all characters except for those in CHARS. If CHARSis empty, it matches all characters.

[^]应该匹配任何字符,包括换行符。[^煤焦]所有字符匹配除了那些在煤焦。如果CHARS为空,则匹配所有字符。

JavaScript example:

JavaScript 示例:

/a[^]*Z/.test("abcxyz ##代码##\r\n\t012789ABCXYZ") // Returns ‘true'.