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

时间:2021-06-04 09:37:53

The following should be matched:

应配合下列各点:

AAA123ABCDEFGH123XXXX123

can I do: ".*123" ?

我能做的事:”。* 123”?

6 个解决方案

#1


373  

Yes, you can. That should work.

是的,你可以。这应该工作。

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

#2


40  

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

是的,这是可行的,但请注意这一点。将不会匹配新行,除非您在编译表达式时传递DOTALL标志:

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

#3


9  

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:

有很多复杂的regex测试和开发工具,但是如果您只是想要在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.

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

See also

#4


7  

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

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

#5


2  

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

#6


2  

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

正则表达式。{ 3 }。这将匹配所有字符期望的新行。

#1


373  

Yes, you can. That should work.

是的,你可以。这应该工作。

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

#2


40  

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

是的,这是可行的,但请注意这一点。将不会匹配新行,除非您在编译表达式时传递DOTALL标志:

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

#3


9  

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:

有很多复杂的regex测试和开发工具,但是如果您只是想要在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.

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

See also

#4


7  

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

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

#5


2  

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

#6


2  

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

正则表达式。{ 3 }。这将匹配所有字符期望的新行。