简体   繁体   中英

Java Regular Expressions: How to express a string that can be ANYTHING?

I've been working with an online regular expression tester to develop a Java regular expression.

What I would like is a regular expression that will match a particular pattern at the start of a strong, a particular pattern at the end of a string, and ANYTHING in between in that string.

For example it would file names structured like this

^StartOfAFileName_middleOfAFileName_EndOfAFileName.java$

The "_middleOfAFileName_" can literally be anything.

I tried this something similar to this in the regular expression tester, but it failed:

^StartOfAFileName_**\***_EndOfAFileName.java$

Any idea what the Java regex equivalent of a "*" or any string/any pattern would be?

It is .* . The . is "any character and the * is any number of them.

https://docs.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html

Here it is in code/context:

String str = "StartOfAFileName_middleOfAFileName_EndOfAFileName.java";
String pattern = "^StartOfAFileName.*EndOfAFileName.java$";
System.out.println(str.matches(pattern) ? "\nMATCHES" : "\nDOESN'T MATCH");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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