I have a String which is formatted as such
[dgdds,dfse][fsefsf,sefs][fsfs,fsef]
How would I use Regex to quickly parse this to return an ArrayList with each value containing one "entry" as such?
ArrayList <String>:
0(String): [dgdds,dfse]
1(String): [fsefsf,sefs]
2(String): [fsfs,fsef]
Really stuck with this, any help would be great.
How about
String myData = "[dgdds,dfse][fsefsf,sefs][fsfs,fsef]";
List<String> list = new ArrayList<>(Arrays.asList(myData
.split("(?<=\\])")));
for (String s : list)
System.out.println(s);
Output:
[dgdds,dfse]
[fsefsf,sefs]
[fsfs,fsef]
This regex will use look behind mechanism to split on each place after ].
You should try this regex :
Pattern pattern = Pattern.compile("\\[\\w*,\\w*\\]");
Old, easy, awesome way :)
String s = "[dgdds,dfse][fsefsf,sefs][fsfs,fsef]";
String[] token = s.split("]");
for (String string : token) {
System.out.println(string + "]");
}
You can use simple \[.*?\] regex, which means: match a string starting with [, later zero or more characters (but as short as possible, not greedly, that's why the ? in .*?), ending with ].
This works, you can test it on Ideone:
List<String> result = new ArrayList<String>();
String input = "[dgdds,dfse][fsefsf,sefs][fsfs,fsef]";
Pattern pattern = Pattern.compile("\\[.*?\\]");
Matcher matcher = pattern.matcher(input);
while (matcher.find())
{
result.add(matcher.group());
}
System.out.println(result);
Output:
[[dgdds,dfse], [fsefsf,sefs], [fsfs,fsef]]
You may need to do it in two passes:
(1) Split out by the brackets if it's just a 1D array (not clear in the question):
String s = "[dgdds,dfse][fsefsf,sefs][fsfs,fsef]";
String[] sArray = s.split("\\[|\\]\\[|\\]");
(2) Split by the commas if you want to also divide, say "dgdds,dfse"
sArray[i].split(",");
We can use split(regex) function directly by escaping "]": "\\]" and then use it as the regex for pattern matching:
String str = "[dgdds,dfse][fsefsf,sefs][fsfs,fsef]";
String bal[] = str.split("\\]");
ArrayList<String>finalList = new ArrayList<>();
for(String s:bal)
{
finalList.add(s+"]");
}
System.out.println(finalList);
Split using this (?:(?<=\])|^)(?=\[) might work if there are nothing between ][
Related
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 am having a hard time figuring with out. Say I have String like this
String s could equal
s = "{1,4,204,3}"
at another time it could equal
s = "&5,3,5,20&"
or it could equal at another time
s = "/4,2,41,23/"
Is there any way I could just extract the numbers out of this string and make a char array for example?
You can use regex for this sample:
String s = "&5,3,5,20&";
System.out.println(s.replaceAll("[^0-9,]", ""));
result:
5,3,5,20
It will replace all the non word except numbers and commas. If you want to extract all the number you can just call split method -> String [] sArray = s.split(","); and iterate to all the array to extract all the number between commas.
You can use RegEx and extract all the digits from the string.
stringWithOnlyNumbers = str.replaceAll("[^\\d,]+","");
After this you can use split() using deliminator ',' to get the numbers in an array.
I think split() with replace() must help you with that
Use regular expressions
String a = "asdf4sdf5323ki";
String regex = "([0-9]*)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(a);
while (matcher.find())
{
String group = matcher.group(1);
if (group.length() > 0)
{
System.out.println(group);
}
}
from your cases, if the pattern of string is same in all cases, then something like below would work, check for any exceptions, not mentioned here :
String[] sArr= s.split(",");
sArr[0] = sArr[0].substring(1);
sArr[sArr.length()-1] =sArr[sArr.length()-1].substring(0,sArr[sArr.length()-1].length()-1);
then convert the String[] to char[] , here is an example converter method
You can use Scanner class with , delimiter
String s = "{1,4,204,3}";
Scanner in = new Scanner(s.substring(1, s.length() - 1)); // Will scan the 1,4,204,3 part
in.useDelimiter(",");
while(in.hasNextInt()){
int x = in.nextInt();
System.out.print(x + " ");
// do something with x
}
The above will print:
1 4 204 3
I have a string in an inconvenient format. Here is an example:
(Air Fresheners,17)->(Chocolate Chips,14)->(Juice-Frozen,24)
I need to go through this string and extract only the first items in the parenthesis. So using the snippet from above as input, I would like my code to return:
Air Fresheners
Chocolate Chips
Juice-Frozen
Note that some of the items have - in the name of the item. These should be kept and included in the final output. I was trying to use:
Scanner.useDelimiter(insert regex here)
...but I am not having any luck. Other methods of accomplishing the task are fine, but please keep it relatively simple.
I know this is old and I'm no expert but can't you use replaceAll? As below:
String s = "(Air Fresheners,17)->(Chocolate Chips,14)->(Juice-Frozen,24)".replaceAll("(->)|[\\(\\)]|\\d+","");
for (String str : s.split(","))
{
System.out.println(str);
}
Try this one
Use regex to split on the basis of )->(
String s="(Air Fresheners,17)->(Chocolate Chips,14)->(Juice-Frozen,24)";
Pattern regex = Pattern.compile("\\)->\\(");
Matcher regexMatcher = regex.matcher(s);
int i=0;
while (regexMatcher.find()) {
System.out.println(s.substring(i+1,regexMatcher.start()));
i=regexMatcher.end()-1;
}
System.out.println(s.substring(i+1,s.length()-1));
Try String.split() method
String s = "(Air Fresheners,17)->(Chocolate Chips,14)->(Juice-Frozen,24)";
for (String str : s.substring(1, s.length() - 1).split("\\)->\\(")) {
System.out.println(str);
}
This can be done with regular expressions. Where we match ([^,)(]*) matches any name that do not contain brackets or commas, ,\\d+\\) matches the ,14) part and (?:->)? matches possible -> after the tuple. We use group(1) to get the name (group(0) returns the whole tuple (Air Fresheners,17)->
List<String> ans = new ArrayList<>();
Matcher m = Pattern.compile("\\(([^,)(]*),\\d+\\)(?:->)?").matcher(str);
while(m.find()){
String s = m.group(1);
ans.add(m.group(1));
}
Given (Air Fresheners,17)->(Chocolate Chips,14)->(Juice-Frozen,24), this program returns [Air Fresheners, Chocolate Chips, Juice-Frozen]
(Air Fresheners,17)->(Chocolate Chips,14)->(Juice-Frozen,24)
You could think of it as everything between the ( and the ,
So,
\(.*?\,
would match "(Air Fresheners," (the ? is to make it non-greedy, and stop when it sees a comma)
So if you're keen to use regex, then just match these, and take a substring to get rid of the ( and ,
I would first go through using )-> as the delimiter. On each scanner.next() get rid of the first character (the parenthesis) using substring, and then place a second scanner on that string that uses , as the delimiter. In code this would look something like:
Scanner s1 = new Scanner(string).useDelimiter("\\s*)->\\s*");
while(s1.hasNext())
{
Scanner s2 = new Scanner(s1.next).useDelimiter("\\s*,\\s*");
System.out.println(s2.next.substring(1));
}
I have trouble splitting string based on regex.
String str = "1=(1-2,3-4),2=2,3=3,4=4";
Pattern commaPattern = Pattern.compile("\\([0-9-]+,[0-9-]+\\)|(,)") ;
String[] arr = commaPattern.split(str);
for (String s : arr)
{
System.out.println(s);
}
Expected output,
1=(1-2,3-4)
2=2
3=3
4=4
Actual output,
1=
2=2
3=3
4=4
This regex would split as required
,(?![^()]*\\))
------------
|->split with , only if it is not within ()
This isn't well suited for a split(...). Consider scanning through the input and matching instead:
String str = "1=(1-2,3-4),2=2,3=3,4=4";
Matcher m = Pattern.compile("(\\d+)=(\\d+|\\([^)]*\\))").matcher(str);
while(m.find()) {
String key = m.group(1);
String value = m.group(2);
System.out.printf("key=%s, value=%s\n", key, value);
}
which would print:
key=1, value=(1-2,3-4)
key=2, value=2
key=3, value=3
key=4, value=4
You will have to use some look ahead mechanism here. As I see it you are trying to split it on comma that is not in parenthesis. But your regular expressions says:
Split on comma OR on comma between numbers in parenthesis
So your String gets splitted in 4 places
1) (1-2,3-4)
2-4) comma
String[] arr = commaPattern.split(str);
should be
String[] arr = str.split(commaPattern);
I'm trying to split the string with double pipe(||) being the delimiter.String looks something like this:
String str ="user#email1.com||user#email2.com||user#email3.com";
i'm able to split it using the StringTokeniser.The javadoc says the use of this class is discouraged and instead look at String.split as option.
StringTokenizer token = new StringTokenizer(str, "||");
The above code works fine.But not able to figure out why below string.split function not giving me expected result..
String[] strArry = str.split("\\||");
Where am i going wrong..?
String.split() uses regular expressions. You need to escape the string that you want to use as divider.
Pattern has a method to do this for you, namely Pattern.quote(String s).
String[] split = str.split(Pattern.quote("||"));
You must escape every single | like this str.split("\\|\\|")
try this bellow :
String[] strArry = str.split("\\|\\|");
You can try this too...
String[] splits = str.split("[\\|]+");
Please note that you have to escape the pipe since it has a special meaning in regular expression and the String.split() method expects a regular expression argument.
For this you can follow two different approaches you can follow whichever suites you best:
Approach 1:
By Using String SPLIT functionality
String str = "a||b||c||d";
String[] parts = str.split("\\|\\|");
This will return you an array of different values after the split:
parts[0] = "a"
parts[1] = "b"
parts[2] = "c"
parts[3] = "d"
Approach 2:
By using PATTERN and MATCHER
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String str = "a||b||c||d";
Pattern p = Pattern.compile("\\|\\|");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println("Found two consecutive pipes at index " + m.start());
}
This will give you the index positions of consecutive pipes:
parts[0] = "a"
parts[1] = "b"
parts[2] = "c"
parts[3] = "d"
Try this
String yourstring="Hello || World";
String[] storiesdetails = yourstring.split("\\|\\|");