问题
I have a variable String which contains values i need and splitters. The problem is, the length of the string is variable and the type of splitters as well. They arrive through XML-file.
A string will look like this:
1+"."+20+"."+51+"."+2+"name.jpg"
but can also be:
1+"*"+20+"*"+51+"name.jpg"
The solid factors are: the digits are id's which I need to retrieve. the splitter values will be between "quotes".
the amount of id's is unknown, can be one, can be 200 the value used to split can be everything, but will always be between two quotes.
I was looking for a way to split the string on the ".
" but instead of the dot (.
) give a wildcard, which can be 1 character or multiple.
Note: The value between the quotes can be anything! Doesn't even have to be a single character
回答1:
Try to split by regular expression, i.e. like this:
String regex = "\\+?\"[^\"]*\"\\+?";
System.out.println(Arrays.toString( "1+\".\"+20+\".\"+51+\".\"+2+\"name.jpg\"".split( regex ) ));
System.out.println(Arrays.toString( "1+\"*\"+20+\"*\"+51+\"name.jpg\"".split( regex ) ));
Output:
[1, 20, 51, 2]
[1, 20, 51]
The regex would match any 2 double quotes with non-double quote characters in between and preceeded/followed by optional pluses. You could expand that to allow whitespace as well, e.g. "\\s*\\+?\\s*\"[^\"]*\"\\s*\\+?\\s*"
. The only thing that's not allowed in a splitter would be double quotes.
If you need the name as well, you might try and define the potential splitters in the regex,
e.g. "(\\+?\"[\\.\\*]*\"\\+?)|\\+?\""
Note that in that case you'd have to account for the quotes around the name, i.e. to split 2+"name.jpg"
you have to add the alternative \+?"
(double quotes preceded by an optional plus).
Update:
Additional examples (input -> output)
5+".."+272+"..."+21+"splitter"+2+"name.jpg" --> [5, 272, 21, 2]
444+"()"+0+"abc"+51+"__"+2+"name.jpg" --> [444, 0, 51, 2]
1+"."+20+"."+51+"."+2+"name.jpg" --> [1, 20, 51, 2]
1+"*"+20+"*"+51+"name.jpg" --> [1, 20, 51]
回答2:
hmm can't you try something like this:
String oldStr=1+"."+20+"."+51+"."+2+"name.jpg";
String newStr= oldStr.replace("name.jpg",""); // or you can use regex such as : oldStr.replaceAll("(\w+.\w+)","");
String[] array;
array=newStr.split(".");
if(array==null || array.length==0){
array=newStr.split("*");
}
回答3:
So, just that I get it right, possible filenames / string values are:
1.20.51.2name.jpg
1*20*51*name.jpg
Right?
So more general you could say: Some digits of unknown amount, seperated by a non-digit character?
You could execute a RegEx statement onto each String: \d+
.
If executed globaly, you will get a list of each number. So for
1.20.51.2name.jpg
I got
1, 20, 51, 2
回答4:
Using this :
String x = 1+"."+20+"."+51+"."+2+"name.jpg";
String y = 1+"*"+20+"*"+51+"name.jpg";
System.out.println(Arrays.toString(x.split("\\.|\\*")));
System.out.println(Arrays.toString(y.split("\\.|\\*")));
Will give you the following output:
[1, 20, 51, 2name, jpg]
[1, 20, 51name, jpg]
来源:https://stackoverflow.com/questions/25140019/splitting-string-with-wildcard