<element>
<Argument Name="AWSAccessKeyId" Value="APOEIUVWIE8E78E6"></Argument>
<Argument Name="SearchIndex" Value="Apparel"></Argument>
<LegalDisclaimer>Leriya Fashion products,on amazon.in."Leriya Fashion" in search.</LegalDisclaimer>
</element>
I want to replace only quotes in this word ("Leriya Fashion").I have tried many regular expression but they replace all the quotes.Right now we know this word but what if we don't know the actual word.
"|[a-z]$$|"$
"|"+\s
I want to replace it with blank or space. And the main problem is occurred when we convert this xml to json. Because json take this double quoted as value but in actual its not a value its just a name which is double quoted.So for me its very tough to replace this quote with blank in json thats why I'm trying to replace this in xml file.
If it's only "Leriya Fashion", then why not just use String::replace
str = str.replace("\"Leriya Fashion\"", "Leriya Fashion");
I'm assuming you just want to remove the quotes.
Lambda replaceAll
str = Pattern.compile("(.)\"([^\"\r\n]*\"").matcher(str)
.replaceAll(mr -> mr.group(1).equals("=") ? mr.group()
: mr.group(1) + mr.group(2));
This will replace all quotes from any text "..." from non-HTML-attributes (="...").
The dangerous assumption is that quotes only appear in pairs.
Related
I have a CSV file below from one of the system.
""demo"",""kkkk""
""demo " ","fg"
" " demo" "
"demo"
"value1","" frg" ","vaue5"
"val3",""tttyy " ",""hjhj","ghuy"
Objective is get all the 2 pair double quotes removed and only one set of double quote is allowed like below. The spaces between the sets of double quote is not a fixed value. This has to be handled in a Java program using replaceAll
function in Java
"demo","kkkk"
"demo","fg"
"demo"
"demo"
"value1","frg","vaue5"
"val3","tttyy","hjhj","ghuy"
I tired this on regex101 with "[ ]*" and it works for PHP>=7.3 version but not in Java.
Also tried [\"][\"]|[^\"]\s+[\"] but still not getting desired output. Any suggestion please for the regular expression which can be used in Java program?
Based on shown sample data, you can use:
String repl = str.replaceAll("(?:\\h*\"){2}\\h*", "\"");
RegEx Demo
RegEx Details:
(?:\h*\"){2}: Match a pair of double quotes that have 0 or more whitespaces between them
\h*: Match 0 or more whitespace
Replacement is just a "
I want to match a string that has "json" (occurs more than 2 times) and without string "from" between two "json".
For example(what I want the string match or not):
select json,json from XXX -> Yes
select json from json XXXX -> No
select json,XXXX,json from json XXX -> Yes
Why the third is matching because I just want two "json" string occurs without "from" inside between it.
After learning regex lookbehind, I'm write the regex like this:
select.*json.*?(?<!from)json.*from.*
I'm using regex lookbehind to except the from string.
But after test, I find this regex match the string "select get_json_object from get_json_object" too.
What wrong for my regex? Any suggestion is appreciated.
You need to use tempered greedy token for achieving this. Use this regex,
\bjson\b(?:(?!\bfrom\b).)+\bjson\b
This expression (?:(?!\bfrom\b).)+ will match any text that does not contain from as a whole word inside it.
Regex Demo
For matching the whole line, you can use,
^.*\bjson\b(?:(?!\bfrom\b).)+\bjson\b.*$
Like you wanted in your post, this regex will match the line as long as it finds a string where a from does not appear between two jsons
Regex Demo with full line match
Edit:
Why OP's regex select.*json.*?(?<!from)json.*from.* didn't work as expected
Your regex starts matching with select and then .* matches as much as possible, while making sure it finds json ahead followed by some optional characters and then again expects to find a json string then .* matches again some characters then expects to find a from and finally using .* zero or more optional characters.
Let's take an example string that should match.
select json from json json XXXX
It has two json string without from in between so it should match but it doesn't, because in your regex, the order or presence of json and from is fixed which is json then again json then from which is not the case in this string.
Here is a Java code demo
List<String> list = Arrays.asList("select json,json from XXX","select json from json XXXX","select json,json from json XXX","select json from json json XXXX");
list.forEach(x -> {
System.out.println(x + " --> " + x.matches(".*\\bjson\\b(?:(?!\\bfrom\\b).)+\\bjson\\b.*"));
});
Prints,
select json,json from XXX --> true
select json from json XXXX --> false
select json,json from json XXX --> true
select json from json json XXXX --> true
I have a tag like following, I want to replace the USERNAME in this, here user name is dynamic value:
<ns3:AgentName xmlns:ns3="http://example.com">USERNAME</ns3:AgentName>
I want to replace this tag with
<ns3:AgentName>XXXXXXX</ns3:AgentName>
I tried the regex like following:
<ns3:AgentName.*</ns3:AgentName>
<ns3:AgentName xmlns:ns3="http://example.com">.*</ns3:AgentName>
Nothing worked for me
This worked for me when I tested it at RegexPlanet.
My regular expression was:
(\<ns3:AgentName) xmlns:ns3="http://example.com"(\>).*(\</ns3:AgentName\>)
If you want to replace USERNAME with x's, my replacement was:
$1$2xxxxxx$3
If you want to put USERNAME in the replacement, I'd make my regular expression this:
(\<ns3:AgentName) xmlns:ns3="http://example.com"(\>.*)(\</ns3:AgentName\>)
and my replacement this:
$1$2$3
I have an XML represented in String. I need to replace all the special characters in the Attribute values with the Escape Characters.
For Ex:
I want to convert 1st one to the second one as following.
<r1 c1=\"01\" c168=\"<A_ATTR><Updates A_VALUE="959" /><Current A_VALUE="100" /></A_ATTR>\"/>
<r1 c1=\"01\" c168=\"<A_ATTR><Updates A_VALUE="959" /><Current A_VALUE="100" /></A_ATTR>\"/>
This questions is similar to the below one : But I need to escape the attribute values. Please advise.
Escape xml characters within nodes of string xml in java
Use string replace function to replace the required character by the encoding. Example below
if your xml string is s then
s = s.replace("<", "<");
s = s.replace(">", ">");
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("''*","''");