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

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 "\\."

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

How I can fix it this "replaceAll" error? [duplicate]

This question already has answers here:
Java Regex matching between curly braces
(5 answers)
Closed 3 years ago.
How I can fix it?
String replace1 = WEBSITE.replaceAll("{fromNumber}", number);
this character "{" error in replaceAll function. Thank you
As #Stephen C has already explained replaceall method's first argument is a regex.
Looks you are trying to replace {fromNumber} simple string with a given number. So instead of using replaceall use replace method which accepts a string as a first argument.
String replace1 = WEBSITE.replace("{fromNumber}", number);
I is not working because '{' is a regex meta-character and replaceAll is using it as so. If you want to replace all "{fromNumber}" from you String then you have to :
String replace1 = WEBSITE.replaceAll("\{fromNumber\}", number);
But if you just have to replace one then you can go with #lahiruk's answer and use
String replace1 = WEBSITE.replace("{fromNumber}", number);
Something to add here , you can use replace any number of times if you know how many times your String will contain the String to be replaced.
For more info
Syntax of regexp.
String.repaceAll()

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.

Regex to remove Entire String from sentence not substring [duplicate]

This question already has an answer here:
scala exactly matching a word in a given string
(1 answer)
Closed 6 years ago.
I am very new to regular expression. I want to replace string from sentence using regular expression in scala or java.
Ex.
"I am new to scala and scalapark is differnt"
I want to remove "scala" string from this statement not "scalapark".
"I am new to and scalapark is differnt"
How can I perform this using regex.
Thanks in advance
You could try this
String s = "I am new to scala and scalapark is differnt";
s = s.replaceAll("\\bscala\\b", "");
Explanation
\\b means word boundary
scala just matches scala

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

Categories