Split only upto 5 comma appear in Java - java

I need to split the below input strings up to the 5th comma. But I need the strings with comma also like in output. I tried
(?<=\G\d+,\d+,\d+,\d+,\d+)
using regex. But it removes the comma in the 5th position.
String data = "12,23,34,45,56,78,9,";
String[] array = data.split("(?<=\\G\\d+,\\d+,\\d+,\\d+,\\d+)"); //Magic :)
// to reveal magic see explanation below answer
for(String s : array){
System.out.println(s); //output = [12,23,34,45,56][78,9,]
}
Can someone try to give the solution which i expected in below output?
input string[] = 12,23,34,45,56,78,9,`enter code here`
output = [12,23,34,45,56,][78,9,]
Thanks in advance

instead of using the split() method, you could try to match with this pattern:
\\G((?:\\d+,?){5}|(?:\\d+,?)+)

or this
(\d+,){5}(\d+,)*
explained
(\d+,){5}...match 'digit(s),' 5 times
(\d+,)*...match remaining portion

You can try the regular expression:
(?<=^\d+,\d+,\d+,\d+,\d+,)
private static final Pattern REGEX_PATTERN =
Pattern.compile("(?<=^\\d+,\\d+,\\d+,\\d+,\\d+,)");
public static void main(String[] args) {
String input = "12,23,34,45,56,78,9,";
System.out.println(java.util.Arrays.toString(
REGEX_PATTERN.split(input)
)); // prints "[12,23,34,45,56,, 78,9,]"
}

Related

I want to split a string with multiple whitespaces using split() method?

This program is to return the readable string for the given morse code.
class MorseCode{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String morseCode = scanner.nextLine();
System.out.println(getMorse(morseCode));
}
private static String getMorse(String morseCode){
StringBuilder res = new StringBuilder();
String characters = new String(morseCode);
String[] charactersArray = characters.split(" "); /*this method isn't
working for
splitting what
should I do*/
for(String charac : charactersArray)
res.append(get(charac)); /*this will return a string for the
corresponding string and it will
appended*/
return res.toString();
}
Can you people suggest a way to split up the string with multiple whitespaces. And can you give me some example for some other split operations.
Could you please share here the example of source string and the result?
Sharing this will help to understand the root cause.
By the way this code just works fine
String source = "a b c d";
String[] result = source.split(" ");
for (String s : result) {
System.out.println(s);
}
The code above prints out:
a
b
c
d
First, that method will only work if you have a specific number of spaces that you want to split by. You must also make sure that the argument on the split method is equal to the number of spaces you want to split by.
If, however, you want to split by any number of spaces, a smart way to do that would be trimming the string first (that removes all trailing whitespace), and then splitting by a single space:
charactersArray = characters.trim().split(" ");
Also, I don't understand the point of creating the characters string. Strings are immutable so there's nothing wrong with doing String characters = morseCode. Even then, I don't see the point of the new string. Why not just name your parameter characters and be done with it?

Split a string using split method

I have tried to split a string using split method, but I'm facing some problem in using split method.
String str="1-DRYBEANS,2-PLAINRICE,3-COLDCEREAL,4-HOTCEREAL,51-ASSORTEDETHNIC,GOURMET&SPECIALTY";
List<String> zoneArray = new ArrayList<>(Arrays.asList(zoneDescTemp.split(",")));
Actual output :
zoneArray = {"1-DRYBEANS","2-PLAINRICE","3-COLDCEREAL","4-HOTCEREAL","51-ASSORTEDETHNIC","GOURMET&SPECIALTY"}
Expected output :
zoneArray = {"1-DRYBEANS","2-PLAINRICE","3-COLDCEREAL","4-HOTCEREAL","51-ASSORTEDETHNIC,GOURMET&SPECIALTY"}
Any help would be appreciated.
Use split(",(?=[0-9])")
You are not just splitting by comma, but splitting by comma only if it is followed by a digit from 0-9. This is also known as positive lookahead (?=).
Take a look at this code snippet for example:
public static void main(String[] args) {
String str="1-DRYBEANS,2-PLAINRICE,3-COLDCEREAL,4-HOTCEREAL,51-ASSORTEDETHNIC,GOURMET&SPECIALTY";
String[] array1= str.split(",(?=[0-9])");
for (String temp: array1){
System.out.println(temp);
}
}
}
Use a look-ahead within your regex, one that uses comma (not in the look-ahead), followed by a number (in the look-head). \\d+ will suffice for number. The regex can look like:
String regex = ",(?=\\d+)";
For example:
public class Foo {
public static void main(String[] args) {
String str = "1-DRYBEANS,2-PLAINRICE,3-COLDCEREAL,4-HOTCEREAL,51-ASSORTEDETHNIC,GOURMET&SPECIALTY";
String regex = ",(?=\\d+)";
String[] tokens = str.split(regex);
for (String item : tokens) {
System.out.println(item);
}
}
}
what this does is split on a comma that is followed by numbers, but does not remove from the output, the numbers since they are part of the look-ahead.
For more on look-ahead, look-behind and look-around, please check out this relevant tutorial page.

How to split the string after dot and print it to next line

String string = "This is a example.just to verify.please help me.";
if(string.matches("(.*).(.*)"))
{
System.out.println(true);
String[] parts = string.split("\\r?\\n");
for(String part:parts){
System.out.println(part);
}
}
I want to split the string after every dot to the next line. can anyone help me in this. thanks in advance.
use regex "\\."
public static void main(String[] args) {
String string = "This is a example.just to verify.please help me.";
if (string.matches("(.*).(.*)")) {
System.out.println(true);
String[] parts = string.split("\\.");
for (String part : parts) {
System.out.println(part);
}
}
}
output
true
This is a example
just to verify
please help me
Use positive lookbehind. And also in matches function, you need to escape the dot like string.matches(".*\\..*"), since dot is a regex special character which matches any character.
String[] parts = string.split("(?<=\\.)");
or
If you don't want to do a split after the last dot.
String[] parts = string.split("(?<=\\.)(?!$)");
DEMO

String.split() --- How do I treat consecutive delimiters as one?

For two sample strings in variable temp such as these:
(1) "|RYVG|111|9|"
(2) "|RYVG|111||9|"
I want to do the following:
String splitRating[] = temp.split("\\|",);
But I want the result to be the same, which is:
splitrating[0] = ""
splitrating[1] = "RYVG"
splitrating[2] = "111"
splitrating[3] = "9
This means that I need to treat that double "|" as one delimiter. Is there any way to do this while still using String.split()?
Add a + to match one or more instances of the pipe:
temp.split("\\|+");
Try this:-
String splitRating[] = temp.split("\\|+");
Yes it is possible
class Split
{
public static void main(String[] args)
{
String temp="|RYVG|111||9|";
String splitRating[] = temp.split("\\|+");
for(String split:splitRating){
System.out.println(split);
}
}
}
StringUtils split method considers the consecutive delimiters as one delimiter.
org.apache.commons.lang.StringUtils.split("|");

Split each string from a paragraph

I am trying to split each string from a paragraph, which has proper grammar based punctuation delimiters like ,.!? or more if any.
I am trying to achieve this using Java. Here is my code.
private void printWords(String inputString) {
String[] x = inputString.split("[.!,\\s]");
for(String temp: x){
System.out.println(temp);
}
}
Sample input String:
He is srk. Oh! I am a very good friend of srk.
My output:
He
is
srk
Oh
I
am
a
very
good
friend
of
srk
There is a problem here, It is having spaces as shown in the output. What should be my regular expression to split strings in any given paragraph, without spaces in the output.
You need to add a + to make your expression match one or more characters:
String[] x = inputString.split("[.!,\\s]+");
What about:
String[] x = inputString.split("\\W+");

Categories