简体   繁体   中英

regular expression that starts with an underscore but not contains any underscore

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$", "");

How 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.

我相信这个正则表达式应该有效:

String repl = str.replaceFirst("_[^_]+$", "");

The ^ character can be used as "exclusive", ie 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 (ie change ABC_.XML to ABC ), and remove the underscore in that case, change + (1 or more) to * (0 or more).

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