Split a mathematical function containing using signs in java [duplicate] - java

This question already has answers here:
How to Split a mathematical expression on operators as delimiters, while keeping them in the result?
(5 answers)
Closed 4 years ago.
I want to split a mathematical function by the sign of the variables in it like this :
input--> x-5y+3z=10
output--> [x,-5y,+3z,=10]
this code does not work in the way i want :
String function = "x-5y+3z=10";
String split = function.split("=|-|\\+");
the output of the array is :
[x,5y,3z,10]
so what is the correct regex for this ?

The "problem" using split is that the delimiter used will be removed, because it'll takt the parts that are between this delimiter, you need a pattern that is non-capturing or with a simple lookahead : match something wich is before something else
The pattern (?=[-+=]) would work, it'll take the part that starts with a -+= symbol without removing it :
String function = "x-5y+3z=10";
String[] split = function.split("(?=[-+=])");
System.out.println(Arrays.toString(split)); //[x, -5y, +3z, =10]
Some doc on Lookahead

Related

Split by : but not :: [duplicate]

This question already has answers here:
Regexp to remove specific number of occurrences of character only
(2 answers)
Closed 2 years ago.
I was wondering how I could split a String by : but not :: using String#split(String)
I am using Java if it makes a difference.
I looked around a lot and I couldn't find anything, and I'm not familiar with Regex...
Example:
coolKey:cool::value should return ["coolKey", "cool::value"]
cool::key:cool::value should return ["cool::key", "cool::value"]
You could try splitting on (?<!:):(?!:):
String input = "cool::key:cool::value";
String[] parts = input.split("(?<!:):(?!:)");
System.out.println(Arrays.toString(parts));
This prints:
[cool::key, cool::value]
The regex used here says to split when:
(?<!:) the character which precedes is NOT colon
: split on colon
(?!:) which is also NOT followed by colon

Replacing Regular expression matches in Java [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I want to replace &sp; in the string below with Z.
Input text : ABCD&sp;EF&p;GHIJ&bsp;KL
Output text : ABCDZEFZGHIZKL
Can anyone tell me how to replace the every instance of &\D+; using java regular expression?
I am using /(&\D+;)?/ but it doesn't work.
Use String#replaceAll.
You also should use the ? modificator to +:
String str = "ABCD&sp;EF&p;GHIJ&bsp;KL";
String regex = "&\\D+?;";
System.out.println (str.replaceAll(regex,"Z"));
This should work
Match the initial &, then all characters that are not the tailing ;, then that tailing ; like so: &[^;]+; If not matching numbers (as suggested by your example with \D) is a requirement, add the numbers to the negated character set: [^;0-9] To make it replace all occurrences, add the global flag g. The site regexr.com is a handy tool to create regexes.
Edit: Sorry, I initially read your question wrong.

Android Spliting the string to array [duplicate]

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I want to split an android string to smaller ones with any | char.
Just imagine I have this long string :
This|is|a|long|string|in|java
So, I wanna split it. I need to get a array in output with this values :
[1]=>"This"
[2]=>"is"
[3]=>"a"
[4]=>"long"
[5]=>"string"
[6]=>"in"
[7]=>"java"
I have tried :
separated = oldstring.split("|");
But, i didn't give me the thing i need!
How can i do that? Any code can do that?
Note that String's split() method take regex as a param. Not string.
public String[] split(String regex)
Since | is a meta character, and it's have a special meaning in regex.
It works when you escape that.
String separated[] = oldstring.split("\\|");

Regex, trim multiple characters? [duplicate]

This question already has answers here:
Removing repeated characters in String
(4 answers)
Closed 8 years ago.
Lets say I have a string:
tttteeeeeeessssssttttttt
Using the power of regex, how can that string be turned into:
test
At first look it seems easy to do, but the current code (not regex) I have for it is not behaving well and im pretty sure regex is the way to go.
You can use:
str = str.replaceAll("([A-Za-z])\\1+", "$1");
RegEx Demo
Use string.replaceAll function.
strng.replaceAll("(.)\\1+", "$1");
The above regex captures the first character in the sequence of same characters and matches all the following one or more characters (which must be same as the one inside the capturing group) . Replacing those characters with the character inside group index 1 will give you the desired output.
Example:
System.out.println("tttteeeeeeessssssttttttt".replaceAll("(.)\\1+","$1" ));
Output:
test
(.)(?=\1)
Try this.Replace by empty string.See demo.
https://regex101.com/r/tX2bH4/41
str = str.replaceAll("(.)(?=\\1)", "");

Replace all with a string having regex wild chars [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java String.replaceAll without regex
I have a string and I need to replace some parts of it.
The replacement text contains regex wild chars though. Example:
String target = "Something * to do in ('AAA', 'BBB')";
String replacement = "Hello";
String originalText = "ABCDEFHGIJKLMN" + target + "ABCDEFHGIJKLMN";
System.out.println(originalText.replaceAll(target, replacement));
I get:
ABCDEFHGIJKLMNSomething * to do in ('AAA', 'BBB')ABCDEFHGIJKLMN
Why doesn't the replacement occur?
Because *, ( and ) are all meta-characters in regular expressions. Hence all of them need to be escaped. It looks like Java has a convenient method for this:
java.util.regex.Pattern.quote(target)
However, the better option might be, to just not use the regex-using replaceAll function but simply replace. Then you do not need to escape anything.
String.replaceAll() takes a regular expression and so it's trying to expand these metacharacters.
One approach is to escape these chars (e.g. \*).
Another would be to do the replacement yourself by using String.indexOf() and finding the start of the contained string. indexOf() doesn't take a regexp but rather a normal string.

Categories