最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - regular expression that starts with an underscore but not contains any underscore - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 6

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.

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

发布评论

评论列表(0)

  1. 暂无评论