I have the following string:
I would "surely" like to "go to school".
Now, I would like to split this string at the ellipses, that is i would like to get the following output:
I would
surely
like to
go to school
.
I case you meant quotation mark (") instead of ellipsis, the easiest solution is to use String.split:
String text = "I would \"surely\" like to \"go to school\".";
String[] result = text.split("\"");
Related
My String is like this.
{\\\"692950841314120\\\":[{\\\"type\\\":\\\"ads_management\\\",\\\"call_count\\\":3,\\\"total_cputime\\\":1,\\\"total_time\\\":5,\\\"estimated_time_to_regain_access\\\":0}]}
Since the key here is a variable value I am trying to replace this 692950841314120(or the values which I get from sever) with a constant like ID. My main goal is to parse this as POJO. I have tried using..
string.replaceAll("^[0-9]{15}$","ID")
but due to Slashes I think i am not able to get the desired value. Is there any better way to do this. I know I can do below Code but I don't want any ID123 if I added extra value and distort any other info in JSON.
string.replaceAll("[0-9]{15}","ID")
Strictly speaking, if you have a valid JSON string, you should parse it using something like GSON, rather than using regex. That being said, if you must use regex, you could try removing the starting and ending anchors:
string.replaceAll("[0-9]{15}", "ID")
Or maybe use double quotes instead:
string.replaceAll("\"[0-9]{15}\"", "ID")
It is safer to assume the value is inisde \" and \":.
You can then use
.replaceAll("(\\\\\")[0-9]{15}(\\\\\":)", "$1ID$2")
The regex is (\\")[0-9]{15}(\\":) and it means:
(\\") - match and capture \" substring into Group 1
[0-9]{15} - fifteen digits
(\\":) - Group 2: a \": substring.
The $1 and $2 are placeholders holding the Group 1 and 2 values.
You should use "A word boundary" \b.
Try this.
public static void main(String[] args) {
String input = "{\\\"692950841314120\\\":"
+ "[{\\\"type\\\":\\\"12345678901234567890\\\","
+ "\\\"call_count\\\":3,"
+ "\\\"total_cputime\\\":1,"
+ "\\\"total_time\\\":5,"
+ "\\\"estimated_time_to_regain_access\\\":0}]}";
System.out.println(input.replaceAll("\\b[0-9]{15}\\b", "ID"));
}
output:
{\"ID\":[{\"type\":\"12345678901234567890\",\"call_count\":3,\"total_cputime\":1,\"total_time\":5,\"estimated_time_to_regain_access\":0}]}
I have a string that contains the following:
"Hello my name is (0.9%) and (15%) bye (10.5%) also (C9, B6)"
I want to replaceAll so I get rid of the brackets containing percentages but not the other numbers like so:
"Hello my name is and bye also C9, B6"
Currently I have this but it removes all my numbers, Any idea how I could fix it:
.replaceAll("[\\([0-9]\\%)]","");
Something like this maybe?
"Hello my name is (0.9%) and (15%) bye (10.5%) also (C9, B6)".replaceAll("\\((?:\\d|\\.)+%\\)", "")
Demo
This here also deletes a single whitespace after each parenthesized percentage:
"Hello my name is (0.9%) and (15%) bye (10.5%) also (C9, B6)".replaceAll("\\((?:\\d|\\.)+%\\) ", "")
Gives:
Hello my name is and bye also (C9, B6)
Remove the outer brackets and add matching to digits before and after an optional dot.
.replaceAll("\\([0-9]+?\\.?[0-9]+?\\%\\)", "");
Note: You can change the "+" to "*" if you want to allow a leading or trailing dot.
I have a document with many string like this:
<rdf:type rdf:resource="http://example.com"/>
where http://example.com is not a constant value, it change every time.
The string must become:
<process:valueType rdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI">http://example.com</process:valueType>
How can i do in java?
I solved in this way:
result = result.replaceAll("(<rdf:type rdf:resource=\"([^<]*)\"/>)", "<process:valueType rdf:datatype=\"http://www.w3.org/2001/XMLSchema#anyURI\">$2</process:valueType>");
I would use lookbehind to find rdf:datatype and select everything other than " which is actually the end of the value, something like this:
(?<=datatype=\")[^\"]*
you can do that with lookahead to:
(?<=datatype=\").*(?=\")
I have a String with single quote. I want to replace the single quote with 2 single quotes.
I tried using
String s="Kathleen D'Souza";
s.replaceAll("'","''");
s.replaceAll("\'","\'\'");
s.replace("'","''");
s.replace("\'","\'\'");
But the single quote is not getting replaced with 2 single quotes.
reassign the replaced string to s
String s="Kathleen D'Souza";
s = s.replaceAll("'","''");
Please try
s= "test ' test";
`s.replaceAll("'","\"");` => test " test
`s.replaceAll("'","''");` => test '' test
Strings are immutable. Assign the result of replaceAll to your String:
s = s.replaceAll("'","''");
String s="Kathleen D'Souza";
s= s.replace("'", "''");
Try String#replace(). It will replace all occurrence of single ' with double ''.
Note, with the given solutions successive single quotes will be doubled, so Kathleen D''Souza turns into Kathleen D''''Souza. (I've seen users outsmart themselves like this.) If that is something you are concerned about, you can match successive single quotes with:
s = s.replaceAll("''*","''");
I got the following string to extract some information from:
String: String: String Number;
Right now I'm using the following regex to get the arguments:
(.*?):(.*?):(.*?);$
This way I would get with a Matcher the following output:
group(1) = String
group(2) = String
group(3) = String Number
If I want the number I need to execute another regex on the output of the 3rd group like the following:
([a-zA-Z]* ?([0-9])?$)
Used ont the String String Number this would give me and output like
group(1) = String
group(2) = Number
I thought about combining both steps and use a regex like (.*?):(.*?):([a-zA-Z]* ?([0-9])?);$ on the String: String: String Number;-String. But this does not work and I dont see the reason.
Hwere you go, I added some extra whitespace matching, but this seems to work, you were missing the whitespace between the second : and the following string
^(.*?):\s*(.*?):\s*([a-zA-Z]*\s+([0-9])?);$