Normally with split() it will divide a whole string by one regex.
So if I take String objects= "door,cat,house,trash";
and do objects.split(",") it will split it into an array of String[] objects= {"door","cat","house","trash"}; as you probably know.
But I don't want to separate it with every comma. I want to separate it with every 15th item. So my list would have String[] objects= {"door,cat,house,trash...obj15","obj1,obj2,obj3...obj15"};
Does that make sense? How would I go about doing that?
split() is not good for that. Use a find() loop instead.
Demo (Java 4+)
String input = "door,cat,house,trash,e5,f6,g7,h8,i9,j10,k11,l12,m13,n14,o15,p16,q17,r18,s19,t20";
Pattern p = Pattern.compile("(?=.)((?:[^,]*,){0,4}[^,]*),?");
List<String> result = new ArrayList<>();
for (Matcher m = p.matcher(input); m.find(); )
result.add(m.group(1));
for (String s : result)
System.out.println('"' + s + '"');
Demo (Java 9+)
String input = "door,cat,house,trash,e5,f6,g7,h8,i9,j10,k11,l12,m13,n14,o15,p16,q17,r18,s19,t20";
String[] result = Pattern.compile("(?=.)((?:[^,]*,){0,4}[^,]*),?").matcher(input)
.results().map(m -> m.group(1)).toArray(String[]::new);
Arrays.stream(result).forEach(s -> System.out.println('"' + s + '"'));
Output
"door,cat,house,trash,e5"
"f6,g7,h8,i9,j10"
"k11,l12,m13,n14,o15"
"p16,q17,r18,s19,t20"
You should of course change {0,4} to {0,14} if you want 15 values per block.
The other answers suggest using complicated regular expressions. I would avoid that. Use Guava to partition the split string into groups of fifteen, and join each group back up with commas:
String[] objects = "door,cat,trash,house,...";
List<String> list = Lists.newArrayList(objects.split(","));
String[] result = Lists.partition(list, 15).stream()
.map(each -> String.join(",", each))
.toArray(String[]::new);
First thing that came to mind is try splitting them with split(",") then loop through them and add any other symbol in the 15th place other than , (e.g $).
Then you split by that (e.g split("$"))
I don't know how effective it is but here you go.
Try the following regex pattern [a-z]+(,[a-z]+){14}. This will match
a list of 15 comma separated words. If I were you, I would switch to using the provided Matcher and Pattern classes to find this regex pattern (instead of using split).
Pattern pattern = Pattern.compile("[a-z]+(,[a-z]+){14}");
Matcher matcher = pattern.matcher("dog,door,cat,other,etc...");
while (matcher.find()) {
System.out.println(matcher.group());
}
I have a string of the following format
A34B56A12B56
And I am trying to sort the numbers into two arrays based on the prefixes.
For example:
Array A: 34,12
Array B: 56,56
What is the simplest way to go about this?
I have tried to use the String Tokenizer class and I am able to extract the numbers, however there is no way of telling what the prefix was. Essentially, I can only extract them into a single array.
Any help would be appreciated.
Thanks!
Andreas seems to have provided a good answer already, but I wanted to practice some regular expressions in Java, so I wrote the following solution that works for any typical alphabetical prefix: (Comments are in-line.)
String str = "A34B56A12B56";
// pattern that captures the prefix and the suffix groups
String regexStr = "([A-z]+)([0-9]+)";
// compile the regex pattern
Pattern regexPattern = Pattern.compile(regexStr);
// create the matcher
Matcher regexMatcher = regexPattern.matcher(str);
HashMap<String, ArrayList<Long>> prefixToNumsMap = new HashMap<>();
// retrieve all matches, add to prefix bucket
while (regexMatcher.find()) {
// get letter prefix (assuming can be more than one letter for generality)
String prefix = regexMatcher.group(1);
// get number
long suffix = Long.parseLong(regexMatcher.group(2));
// search for list in map
ArrayList<Long> nums = prefixToNumsMap.get(prefix);
// if prefix new, create new list with the number added, update the map
if (nums == null) {
nums = new ArrayList<Long>();
nums.add(suffix);
prefixToNumsMap.put(prefix, nums);
} else { // otherwise add the number to the existing list
nums.add(suffix);
}
System.out.println(prefixToNumsMap);
}
Output : {A=[34, 12], B=[56, 56]}
I know that this question can be stupid but I am trying to get some information from text and you are my last hope after last three hours of trying..
DIC: C/40764176 IC: 407641'6
Dekujerne a t8ime se na shledanou
I need to get for example this 40764176
I need to get string with 8-10 length, sometimes there can be some special chars like I,i,G,S,O,ó,l) but I have tried a lot of patterns for this and no one works...
I tried:
String generalDicFormatPattern = "([0-9IiGSOól]{8,10})";
String generalDicFormatPattern = ".*([0-9IiGSOól]{8,10}).*";
String generalDicFormatPattern = "\\b([0-9IiGSOól]{8,10})\\b";
nothing works... do you know where is the problem?
edit:
I use regex in this way:
private List<String> getGeneralDicFromLine(String concreteLine) {
List<String> allMatches = new ArrayList<String>();
Pattern pattern = Pattern.compile(generalDicFormatPattern);
Matcher matcher = pattern.matcher(concreteLine);
while (matcher.find()) {
allMatches.add(matcher.group(1));
}
return allMatches;
}
If your string's pattern is fixed you can use the regex
C/([^\s]{8,10})\sIC:
Sample code:
String s = "DIC: C/40764176 IC: 407641'6";
Pattern p = Pattern.compile("C/([^\\s]{8,10})\\sIC:");
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println(m.group(1)); // 40764176
}
I'm expecting any character (includes the special ones you've shown in examples) but a white space.
May be you can split your string with spaces (string.split('\\s');), then you should have an array like this :
DIC:
C/40764176
IC: 407641'6
...
shledanou
Get the second string, split it using '/', and get the second element.
I hope it helped you.
Tip : you can check after the result using a regex (([0-9IiGSOól]{8,10})
I am trying to get an array of strings, from a lengthy string. Array consist of strings matching between two other strings (??? and ??? in my case). I tried the following code and it's not giving me the expected results
Pattern pattern = Pattern.compile("\\?\\?\\?(.*?)\\?\\?\\?");
String[] arrayOfKeys = pattern.split("???label.missing???sdfjkhsjkdf sjkdghfjksdg ???some.label???sdjkhsdj");
for (String key : arrayOfKeys) {
System.out.println(key);
}
My expected result is:
["label.missing", "some.label"]
Use Pattern.matcher() to obtain a Matcher for the input string, then use Matcher.find() to find the pattern you want. Matcher.find() will find substring(s) that matches the Pattern provided.
Pattern pattern = Pattern.compile("\\?{3}(.*?)\\?{3}");
Matcher m = pattern.matcher(inputString);
while (m.find()) {
System.out.println(m.group(1));
}
Pattern.split() will use your pattern as delimiter to split the string (then the delimiter part is discarded), which is obviously not what you want in this case. Your regex is designed to match the text that you want to extract.
I shorten the pattern to use quantifier repeating exactly 3 times {3}, instead of writing \? 3 times.
I would create a string input with what you're trying to split, and call input.split() on it.
String input = "???label.missing???sdfjkhsjkdf sjkdghfjksdg ???some.label???sdjkhsdj";
String[] split = input.split("\\?\\?\\?");
Try it here:
http://ideone.com/VAmCyu
Pattern pattern = Pattern.compile("\\?{3}(.+?)\\?{3}");
Matcher matcher= pattern.matcher("???label.missing???sdfjkhsjkdf sjkdghfjksdg ???some.label???sdjkhsdj");
List<String> aList = new ArrayList<String>();
while(matcher.find()) {
aList.add(matcher.group(1));
}
for (String key : aList) {
System.out.println(key);
}
Please check following code.
Pattern pxPattern = Pattern.compile("^.*[0-9]+(%|pt|em).*$");
Matcher pxMatcher = pxPattern.matcher("onehellot455emwohellothree");
System.out.println(pxMatcher.matches());
System.out.println(pxMatcher.group(0));
i want to substract string 445em. i am using code for checking css. means i wants to extract
just values like 45em or 50%.
Thanks.
First, the captured group is in group 1, not group 0. Then you need to modify your regex to not consume the numbers and include them in the group. Try:
Pattern pxPattern = Pattern.compile("^.*?([0-9]+(?:%|pt|em)).*$");
Matcher pxMatcher = pxPattern.matcher("onehellot455emwohellothree");
System.out.println(pxMatcher.matches());
System.out.println(pxMatcher.group(1));
EDIT:
To get all values from a string with several, you can use this pattern:
Pattern pxPattern = Pattern.compile("[0-9]+(?:%|pt|em)");
Matcher pxMatcher = pxPattern.matcher("margin: 0pt, 6em, 5%, 2pt");
List<String> propertyValues = new ArrayList<String>();
while (pxMatcher.find()) {
propertyValues.add(pxMatcher.group());
}