Split by : but not :: [duplicate] - java

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

Related

java Complex Regex doesn't work in String.matches() [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 4 years ago.
i have this regex which i want to check if string do contain this structure
none of the strings below match , why?
String s = "T4018.test.12"
or
String s = "T4018.foo.32"
or
String s = "develop"
if(s.matches("T40[0-9][0-9][.][a-zA-Z]+|develop"))
{
//Never matches
}
its do match :
https://regex101.com/r/CSLugb/1
UPDATE
Solved it using Pattern.compile
Maybe you can rewrite the RegEx to "T40\\d{2}\\.\\w+\\.\\d{2}|develop" for matching more exactly. See your 3 test-cases matched in Java

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

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

Why does splitting on a period need double back-slashes? [duplicate]

This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 6 years ago.
I have a String called filename:
filename = "z_cams_c_ecmf_20170217000000_prod_fc_pl_015_aermr04.nc";
When I try to split the filename to get the variable name aermr04.nc, I tried the following:
String varibleName = filename.split("_")[9].split(".")[0];
The above line of code throws an IndexOutOfBoundsException.
Why?
I can get it tow work by using:
String varibleName = filename.split("_")[9].split("\\.")[0];
However, it seems rather silly that I have to fiddle around with such trivial tasks...
Any idea why the 2nd example works? What is the reasoning behind such syntax?
The argument to .split() is treated as a regular expression. "." as a regex matches everything.
To match a period, you need to escape the "." regex as "\\."

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("\\|");

java regex to not get string not ending with dot [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 8 years ago.
I m trying regex to get the strings
starting with # and
not ending with a dot(.)
For that i tried the java code(link here) but this does not show any results -
#(\\w+)*(?<!.(.))*$
The string i m trying is -
This is a test\nAnother #pradyut#test ht#html.com\ntest\n#art\n#cool#paintings#collections
This should return
pradyut
test
cool
The result html ending with a .com should not return.
Regards
You can use this regex:
(?<=#)\w+\b(?!\.)
In Java you have to use:
(?<=#)\\w+\\b(?!\\.)
Regex Demo

Categories