简体   繁体   English

以下划线开头但不包含任何下划线的正则表达式

[英]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 例如, ABC_AA.xml应为ABCABC_AASD_AD_AA.xml应为ABC_AASD_AD

I am thinking about using non-greedy with exlusive ^ symbol. 我正在考虑使用非贪婪的exlusive ^符号。 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 . 它将匹配(并删除) _具有零个或多个非_字符[^_]*并以.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. 任何不符合Pattern输入都将被退回。

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

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). 或者,如果要处理以下划线结尾的文件名( ABC_.XML更改为ABC ),并在该情况下删除下划线,请将+ (1或更多)更改为* (0或更多)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM