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
Related
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()
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
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 "\\."
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)", "");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I split a string with any whitespace chars as delimiters?
Yes, I tried to search it on google and stackoverflow, but no good results. I have a string, lets say: "Lets do some coding in Java" and I would like to got strings (split it on whitespaces):
Lets, do, some, coding, in, Java
I used string.split("\\s") for this, but know I need to use regex instead. Any ideas?
String str = "Hello How are you";
String arrayString[] = str.split("\\s+")
Please use this
to specify space as splitting char, you can pass " " as parameter to String#split.
Example:
String test="Lets do some coding in Java";
for(String token : test.split(" "))
System.out.println(token);