How to split this String using Regex Java - java

As the title says I want to split a string specifically but I donĀ“t know what to put inside the String.split("regex")
Say we have:
String s = "sum := A+B*( 99.1 +.44 444 1234+++)-sum/232.123459"
Now I want to split it to put it this way:
String[] splitted = ["sum"; ":="; "A"; "+"; "B"; "*"; "("; "99.1"; "+"; ".44"; "444"; "1234"; "+"; "+"; "+"; ")"; "-"; "sum"; "/"; "232.123459"]
So, basically I want to split by space, words, the math operators, numbers, the parenthesis, the letters and the number ".44" has to remain this way.
Can you help me?
Thanks in advance

Don't use split(). Use a find() loop.
String regex = "[0-9]+\\.?[0-9]*" + // Match number (e.g. 999 or 999.99)
"|\\.[0-9]+" + // Match number (e.g. .999)
"|[a-zA-Z]\\w*" + // Match identifier
"|:=" + // Match complex operator
"|\\S"; // Match other single non-space, incl. operators: +, -, *, /, (, )
Test
String s = "sum := A+B*( 99.1 +.44 444 1234+++)-sum/232.123459";
String[] splitted = Pattern.compile(regex).matcher(s).results()
.map(MatchResult::group).toArray(String[]::new);
System.out.println(Arrays.toString(splitted));
Output
[sum, :=, A, +, B, *, (, 99.1, +, .44, 444, 1234, +, +, +, ), -, sum, /, 232.123459]

Related

Need help splitting the expression with regex

I have an expression like this.
A AND (B OR (C OR D))
I want the parentheses as a separate string and not combined with C OR D in the output array.
[A, AND, (, B, OR, (, C, OR, D, ), )]
Appending , in place of SPACE and after every ( and before every ) and then using .split(",") would solve my problem.
Is there any way better way to do this by simply using the right regex in the split method ?
How about this:
String input = "A AND (B OR (C OR D))";
String regex = "\\s+|(?<=\\()|(?=\\))";
String[] tokens = input.split(regex);
Which returns:
{A, AND, (, B, OR, (, C, OR, D, ), )}
Explanation:
The regex splits by
One or more spaces
Anything followed by a parenthesis
Anything preceded by a parenthesis
I used positive lookaheads and positive lookbehinds, which are INCREDIBLY useful, so do look them up (no pun intended)
I hope this would help:
"A AND (B OR (C OR D))".split(" +| (?=\\()|(?=\\))|(?<=\\()") #=> [A, AND, (, B, OR, (, C, OR, D, ), )]
+ # splits by whitespaces
(?=\\() # splits by whitespace followed by opening brace: e.g. in " (" it would give you single "(" instead of " " and "(" (like in the next part without whitespace in the beginning)
(?=\\)) # splits by empty string followed by closing brace: e.g. "B)" => ["B", ")"]
(?<=\\)) # splits by empty string preceding by closing brace: e.g. "))"
Search for "Positive lookahead/lookbehind" in regular expressions (personally I use regex101.com).

Splitting a String by number of delimiters

I am trying to split a string into a string array, there might be number of combinations,
I tried:
String strExample = "A, B";
//possible option are:
1. A,B
2. A, B
3. A , B
4. A ,B
String[] parts;
parts = strExample.split("/"); //Split the string but doesnt remove the space in between them so the 2 item in the string array is space and B ( B)
parts = strExample.split("/| ");
parts = strExample.split(",|\\s+");
Any guidance would be appreciated
To split with comma enclosed with optional whitespace chars you may use
s.split("\\s*,\\s*")
The \s*,\s* pattern matches
\s* - 0+ whitespaces
, - a comma
\s* - 0+ whitespaces
In case you want to make sure there are no leading/trailing spaces, consider trim()ming the string before splitting.
You can use
parts=strExample.split("\\s,\\s*");
for your case.

How to split a string by space and some special character in java

Consider the string .more opr (&x NE &m),&n+1
All i need is split this string into following parts .more,opr,(,&x,NE,&m,) , , , &n, +, 1.In short I need to split on spaces and some special symbols like ( ) , and arithmetic operators.
How to write regex expression for split() in java to achieve this.
Split on space or either side of brackets or operators:
str.split(" |(?<=[,()+-])|(?<! )(?=[,()+-])")
The output of:
String str = ".more opr (&x NE &m),&n+1";
System.out.println(Arrays.toString(str.split(" |(?<=[,()+-])|(?<! )(?=[,()+-])")));
is:
[.more, opr, (, &x, NE, &m, ), ,, &n, +, 1]
Or more clearly:
Arrays.stream(str.split(" |(?<=[,()+-])|(?<! )(?=[,()+-])")).forEach(System.out::println);
outputs:
.more
opr
(
&x
NE
&m
)
,
&n
+
1

Java replace strings between two commas

String = "9,3,5,*****,1,2,3"
I'd like to simply access "5", which is between two commas, and right before "*****"; then only replace this "5" to other value.
How could I do this in Java?
You can try using the following regex replacement:
String input = "9,3,5,*****,1,2,3";
input = input.replaceAll("[^,]*,\\*{5}", "X,*****");
Here is an explanation of the regex:
[^,]*, match any number of non-comma characters, followed by one comma
\\*{5} followed by five asterisks
This means to match whatever CSV term plus a comma comes before the five asterisks in your string. We then replace this with what you want, along with the five stars in the original pattern.
Demo here:
Rextester
I'd use a regular expression with a lookahead, to find a string of digits that precedes ",*****", and replace it with the new value. The regular expression you're looking for would be \d+(?=,\*{5}) - that is, one or more digits, with a lookahead consisting of a comma and five asterisks. So you'd write
newString = oldString.replaceAll("\\d+(?=,\\*{5})", "replacement");
Here is an explanation of the regex pattern used in the replacement:
\\d+ match any numbers of digits, but only when
(?=,\\*{5}) we can lookahead and assert that what follows immediately
is a single comma followed by five asterisks
It is important to note that the lookahead (?=,\\*{5}) asserts but does not consume. Hence, we can ignore it with regards to the replacement.
I considered newstr be "6"
String str = "9,3,5,*****,1,2,3";
char newstr = '6';
str = str.replace(str.charAt(str.indexOf(",*") - 1), newstr);
Also if you are not sure about str length check for IndexOutOfBoundException
and handle it
You could split on , and then join with a , (after replacing 5 with the desired value - say X). Like,
String[] arr = "9,3,5,*****,1,2,3".split(",");
arr[2] = "X";
System.out.println(String.join(",", arr));
Which outputs
9,3,X,*****,1,2,3
you can use spit() for replacing a string
String str = "9,3,5,*****,1,2,3";
String[] myStrings = str.split(",");
String str1 = myStrings[2];

Splitting a string keeping some delimiters but removing another

Basically I would like to split a string into an array delimiting by spaces and operators, but keep the operators while removing the spaces
ex. 3 52 9+- 2 3 * /
will be [3][52][9][+][-][2][3][*][/]
The logic you want when splitting is to consume delimiters which are whitespace and to not consume delimiters which are arithmetic symbols. Towards this end, we can use a lookahead to split by symbol, and use plain \\s to split by whitespace and remove it from the result.
String input = "3 52 9+- 2 3 * /";
input = input.replaceAll("([\\+\\-*/])(.)", " $1$2")
.replaceAll("\\s+", " ");
String[] parts = input.split("(?<=[\+\-*/])|\\s")
System.out.println(Arrays.toString(parts));
Output:
[3, 52, 9, +, -, 2, 3, *, /]
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
String input = "3 52 9+- 2 3 * /";
input = input.replaceAll("([\\+\\-*/])", " $1 ").replaceAll("\\s+", " ");
String[] parts = input.split("(?<=[+\\-*/ ])");
List<String> finalList = new ArrayList<String>();
for(String part : parts) {
if(part.trim().length() > 0) {
finalList.add(part);
}
}
System.out.println(finalList);
}
}
Output
[3 , 52 , 9 , +, -, 2 , 3 , *, /]
Try this regex:
([\-]?[^\s\+\-\*\/]+|[\+\-\*\/])
It will select:
[\-]? signed or unsigned
[^\s\+\-\*\/] characters that is neither spaces nor [+ - * /]
or [\+\-\*\/] [+ - * /]
Just match your case.

Categories