Steve Steve - 4 months ago 7
Java Question

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?

Answer

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

Comments