[英]Split string after n amount of digits occurrence
I'm parsing some folder names here.我在这里解析一些文件夹名称。 I have a program that lists subfolders of a folder and parses folder names.我有一个程序可以列出文件夹的子文件夹并解析文件夹名称。
For example, one folder could be named something like this:例如,一个文件夹可以这样命名:
"Folder.Name.1234.Some.Info.Here-ToBeParsed" “Folder.Name.1234.Some.Info.Here-ToBeParsed”
and I would like to parse it so name would be "Folder Name".我想解析它所以名称将是“文件夹名称”。 At the moment I'm first using string.replaceAll() to get rid of special characters and then there is this 4-digit sequence.目前我首先使用 string.replaceAll() 来去除特殊字符,然后是这个 4 位序列。 I would like to split string on that point.我想在这一点上拆分字符串。 How can I achieve this?我怎样才能做到这一点?
Currently my code looks something like this:目前我的代码看起来像这样:
// Parsing string if regex p matches folder's name
if(b) {
//System.out.println("Folder: \" " + name + "\" contains special characters.");
String result = name.replaceAll("[\\p{P}\\p{S}]", " "); // Getting rid of all punctuations and symbols.
//System.out.println("Parsed: " + name + " > " + result);
// If string matches regex p2
if(b2) {
//System.out.println("Folder: \" " + result + "\" contains release year.");
String parsed_name[] = result.split("20"); // This is the line i would like to split when 4-digits in row occur.
//System.out.println("Parsed: " + result + " > " + parsed_name[0]);
movieNames.add(parsed_name[0]);
}
Or maybe there is even easier way to do this?或者也许有更简单的方法来做到这一点? Thanks in advance!提前致谢!
You should keep it simple like this:你应该像这样保持简单:
String name = "Folder.Name.1234.Some.Info.Here-ToBeParsed";
String repl = name.replaceFirst( "\\.\\d{4}.*", "" ).
replaceAll( "[\\p{P}\\p{S}&&[^']]+", " " );
//=> Folder Name
replaceFirst
is removing everything after a DOT and 4 digits replaceFirst
正在删除 DOT 和 4 位数字之后的所有内容replaceAll
is replacing all punctuation and space (except apostrophe) by a single space replaceAll
将所有标点符号和空格(撇号除外)替换为一个空格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.