I have below program:
String one = "Hello I am IN (?)";
String two = one.replaceFirst(" IN (?)", " IN ('one','two')");
System.out.println(two);
The output is:
Hello I am IN ('one','two')(?)
The output I am expecting is:
Hello I am IN ('one','two')
What is the issue? It has to do with replaceFirst method having first argument as a regex.
replaceFirst uses a regular expression as its first argument. ( and ) are meta characters used to denote capturing groups, ? being the non-greedy quantifier
You can either escape meta characters
String two = one.replaceFirst(" IN \\(\\?\\)", " IN ('one','two')");
or (better) using the replace method as mentioned by #RJ
String two = one.replace(" IN (?)", " IN ('one','two')");
Since the first parameter of replaceFirst() is a regex, you need to escape the (?) as (?) has a special meaning in regex(read meta character).
String two = one.replaceFirst(" IN \\(\\?\\)", " IN ('one','two')");
To avoid escaping, you can use the replace() method, which doesn't take a regex as the replace string.
String two = one.replace(" IN (?)", " IN ('one','two')");
Try this one
String one = "Hello I am IN (?)";
String two = one.replaceFirst(" IN \\(\\?\\)", " IN ('one','two')");
System.out.println(two);
Related
I have a string where it contains different combinations of the slashes
{"result":"{\"cov_details\":[{\"issue_date\":\"UNIT
OFFICE,NEYVELI\",\"cov\":\"MCWG\"}],\"dl_number\":\"TN31Y200000784\",\"address\":\"PERIYA COLONY KO
PAVAZHANGUDI VIRUDHACHALAM TK\",\"issue_date\":\"24-03-2010\",\"dob\":\"21-03-
1981\",\"name\":\"VICNESWARAN S\",\"blood_group\":\"\",\"validity\":{\"transport\":\"\",\"non-
transport\":\"4-01-2010 to 23-03-2040\"},\"father\\\/husband\":\"SELVAM\"}","status-
code":"101","request_id":"a2642ae9-2f10-4e9a-9f7e-c3ee1a9a2dbe"}
I want to replace all occurances of a single "" alone but ignore when "" is followed by "/" ( check out the father\\\/husband\ parameter. It should read father\/husband. How can I achieve this in Java?
Just hide "\/" by replacing it with another character sequence like "~~~", then restore it after:
String string = "father\\\\\\/husband\\";
System.out.println("Before\t:\t" + string);
System.out.println
(
"After\t:\t" + string
.replaceAll("\\\\/", "~~~")
.replaceAll("\\\\", "")
.replaceAll("~~~", "\\\\/")
);
Output:
Before : father\\\/husband\
After : father\/husband
public String replaceAll​(String regex, String replacement) accepts regex as the first parameter:
https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/String.html#replaceAll(java.lang.String,java.lang.String)
Fill free to specify any pattern you need:
https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/regex/Pattern.html#sum
I have a string
"#72c02c, #3498db, #e67e22, #9c8061, #4765a0, #79d5b3"
I want to convert it to the following:
"'#72c02c', '#3498db', '#e67e22', '#9c8061', '#4765a0', '#79d5b3'"
Is there any way to do so?
I don't know why everyone is using regex... this needs only a simple text replace:
str = "'" + str.replace(", ", "', '") + "'";
This just puts quotes before/after commas and puts a quote at each end, which is easy to understand.
String s = "#72c02c, #3498db, #e67e22, #e74c3c, #ecf0f1, #9b6bcc, #27d7e7, #9c8061, #4765a0, #79d5b3";
s = s.replaceAll("(#|,)","'$1");
if (s.charAt(s.length()-1) != ',')
s = s + "'";
Might not be the most elegant solution, but it handles the case of the final item in the string not ending with a ,. Will work if it does happen to end in a comma too.
If you are able to operate with it as with the simple String object instance, the you can do it with regex ([#\w]+) and String.replaceAll() method, like:
String testString = "#72c02c, #3498db, #e67e22, #e74c3c, #ecf0f1, #9b6bcc, #27d7e7, #9c8061, #4765a0, #79d5b3";
System.out.println(testString.replaceAll("([#\\w]+)","'$1'"));
Where with ([#\w]+), you just take every group of alpha-numeric characters or #
Then you just replace it with '$1', where $1 is the whole group, which was found.
I have string like this String s="ram123",d="ram varma656887"
I want string like ram and ram varma so how to seperate string from combined string
I am trying using regex but it is not working
PersonName.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(1))).replaceAll("[^0-9]+"));
The correct RegEx for selecting all numbers would be just [0-9], you can skip the +, since you use replaceAll.
However, your usage of replaceAll is wrong, it's defined as follows: replaceAll(String regex, String replacement). The correct code in your example would be: replaceAll("[0-9]", "").
You can use the following regex: \d for representing numbers. In the regex that you use, you have a ^ which will check for any characters other than the charset 0-9
String s="ram123";
System.out.println(s);
/* You don't need the + because you are using the replaceAll method */
s = s.replaceAll("\\d", ""); // or you can also use [0-9]
System.out.println(s);
To remove the numbers, following code will do the trick.
stringname.replaceAll("[0-9]","");
Please do as follows
String name = "ram varma656887";
name = name.replaceAll("[0-9]","");
System.out.println(name);//ram varma
alternatively you can do as
String name = "ram varma656887";
name = name.replaceAll("\\d","");
System.out.println(name);//ram varma
also something like given will work for you
String given = "ram varma656887";
String[] arr = given.split("\\d");
String data = new String();
for(String x : arr){
data = data+x;
}
System.out.println(data);//ram varma
i think you missed the second argument of replace all. You need to put a empty string as argument 2 instead of actually leaving it empty.
try
replaceAll(<your regexp>,"")
you can use Java - String replaceAll() Method.
This method replaces each substring of this string that matches the given regular expression with the given replacement.
Here is the syntax of this method:
public String replaceAll(String regex, String replacement)
Here is the detail of parameters:
regex -- the regular expression to which this string is to be matched.
replacement -- the string which would replace found expression.
Return Value:
This method returns the resulting String.
for your question use this
String s = "ram123", d = "ram varma656887";
System.out.println("s" + s.replaceAll("[0-9]", ""));
System.out.println("d" + d.replaceAll("[0-9]", ""));
I have a String saved in the database like this:
01/01/2014$%^&02/01/2014
I am using the split() java method. Here is my code:
String value = database.getValue(); // this returns the value mentioned above
String values[] = value.split("$%^&");
System.out.println("length is = " + values.length);
System.out.println(values[0]);
The Output:
length is = 1
01/01/2014$%^&02/01/2014
What am I doing wrong?
Use Pattern#quote:
String values[] = value.split(Pattern.quote("$%^&"));
What am I doing wrong?
String#split takes a regex as its argument. Some characters in your String have special meaning. In your case, you want the String representation, not regex. quote does that for you.
Alternative solutions:
Escape the special characters by \\ (Escaping regex is done by \. But in Java, \ is written \\)
Use String#replace that accepts String
You can try this too. Here we can escape special characters.
String value = "01/01/2014$%^&02/01/2014";
String values[] = value.split("\\$%\\^&");
System.out.println("length is = " + values.length);
System.out.println(values[0]);
Out put:
length is = 2
01/01/2014
I need to replace a String which contains white space and periods. I have tried with the following code:
String customerName = "Mr. Raj Kumar";
customerName = customerName.replaceAll(" ", "");
System.out.println("customerName"+customerName);
customerName = customerName.replaceAll(".", "");
System.out.println("customerName"+customerName);
but this results in:
customerName Mr.RajKumar
And
customerName
I am getting the correct customer name from the first SOP, but from second SOP I am not getting any value.
escape the dot, or else it will match any character. This escaping is necessary, because replaceAll() treats the first paramter as a regular expression.
customerName = customerName.replaceAll("\\.", "");
You can do the whole thing with one statement:
customerName = customerName.replaceAll("[\\s.]", "");
use this in your code just for remove periods
customerName = customerName.replaceAll("[.]","");
You can simply use str.replace(".", "") and it will replace all occurences of dot, Remember there is only one difference betwwen replace and replaceAll which is, later uses regex as input string where as first one uses simple character sequence.