Java – regular expressions that begin with an underscore but do not contain any underscores
•
Java
I try to get the name of the file from the last underscore until the end
For example, ABC_ AA. XML should be ABC and ABC_ AASD_ AD_ AA. XML should be ABC_ AASD_ AD
I'm considering using the non greedy exclusive ^ symbol I tried this:
String nameToSearch = testName.replaceAll("_(^(_).)+\\.xml$","");
Solution
How to use simple substrings instead of regular expressions
String nameToSearch = testName.substring(0,testName.lastIndexOf('_'));
Or if not_ You can use
String noSuffix = testName.substring(0,testName.lastIndexOf('.'));//remove ".xml" String nameToSearch = noSuffix.substring(0,testName.lastIndexOf('_'));
But if you really want to use regular expressions, you can try them
testName.replaceAll("_[^_]*[.]xml$","");
It will match (and delete)_ Has zero or more non_ Character [^]* And with End of XML
If not_ You can use_ [^ _] * Optional
testName.replaceAll("(_[^_]*)?[.]xml$","");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码