I am trying to fetch the name of a file without the part 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 am thinking about using non-greedy with exlusive ^
symbol.
I have tried this:
String nameToSearch = testName.replaceAll("_(^(_).)+\\.xml$", "");
I am trying to fetch the name of a file without the part 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 am thinking about using non-greedy with exlusive ^
symbol.
I have tried this:
String nameToSearch = testName.replaceAll("_(^(_).)+\\.xml$", "");
Share
Improve this question
edited Mar 21, 2014 at 17:13
P̲̳x͓L̳
3,6513 gold badges31 silver badges37 bronze badges
asked Mar 21, 2014 at 17:09
bookmonkiebookmonkie
4496 silver badges21 bronze badges
4 Answers
Reset to default 6How about using simple substring instead of regex
String nameToSearch = testName.substring(0, testName.lastIndexOf('_'));
or in case there can be no _
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 regex then you can try with
testName.replaceAll("_[^_]*[.]xml$", "");
which will match (and remove) _
which has zero or more non _
characters [^_]*
and ends with .xml
.
In case there can be no _
you can make _[^_]*
optional with
testName.replaceAll("(_[^_]*)?[.]xml$", "");
Simple.
Use groups and back-references, as such:
String input = "ABC_AASD_AD_AA.xml";
// | using replaceAll to pass regex
// | | group 1: one or more characters, greedy
// | | | underscore
// | | || one or more characters, reluctant
// | | || | escaped dot and extension
// | | || | | back-reference to group 1
System.out.println(input.replaceAll("(.+)_.+?\\.xml", "$1"));
Output
ABC_AASD_AD
Note
Any input not conforming to the Pattern
will be returned as such.
I believe this regex should work:
String repl = str.replaceFirst("_[^_]+$", "");
The ^
character can be used as "exclusive", i.e. to exclude certain characters, only as the first character of a character class inside []
. [^_]
matches any character that's not an underscore. Outside of square brackets, it means "the beginning of the source string".
So you're close. Try this:
String nameToSearch = testName.replaceAll("_[^_]+\\.xml$", "");
Or, if you want to handle file names ending in underscore (i.e. change ABC_.XML
to ABC
), and remove the underscore in that case, change +
(1 or more) to *
(0 or more).