how to replace charactor including double quote? - java

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

Related

What should be regular expression to replace only two quotes?

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

How to split this string in java need regex?

i need to split this string:
COMITATO: TRIESTE Indirizzo legale: VIA REVOLTELLA 39 34139
Trieste (Trieste) Mob.: 3484503368 Fax: 040310096 Sito web: www.csentrieste.it/
the wanted result must be an array like:
{COMITATO:,TRIESTE,Indirizzo legale:,VIA REVOLTELLA 39 34139
Trieste (Trieste) ,Mob.:,3484503368,Fax:,Sito web:,www.csentrieste.it/}
the problem is also that some attribute of string can be missing so i cant split using the header of attribute like "COMITATO:" or "Indirizzo legale:"
example:if "Indirizzo legale:" its missing string will appear like:
COMITATO: TRIESTE Mob.: 3484503368 Fax: 040310096 Sito web: www.csentrieste.it/
Well, this regex will parse your given inputs:
(?<firstname>.*?):\s*(?<lastname>\w+)(?:(?<occupation>[^:]+):\s*(?<address>.+\n.+))?\sMob.:\s*(?<mobile>\d+)\s*Fax:\s*(?<fax>\d+)\s*Sito web:\s*(?<website>.*)
We can salvage some readability and easy access of the results by using named groups. Nothing too clever about the regex, we just crawl through the string, using what static structure we can to anchor the pattern: the colons, the "Mob", "Fax", and "Sito web". Obviously the "maybe missing" address part is optional.
regex demo here

Java Replace String with a variable text

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=\").*(?=\")

java regexp for reluctant matching

need to find an expression for the following problem:
String given = "{ \"questionID\" :\"4\", \"question\":\"What is your favourite hobby?\",\"answer\" :\"answer 4\"},{ \"questionID\" :\"5\", \"question\" :\"What was the name of the first company you worked at?\",\"answer\" :\"answer 5\"}";
What I want to get: "{ \"questionID\" :\"4\", \"question\":\"What is your favourite hobby?\",\"answer\" :\"*******\"},{ \"questionID\" :\"5\", \"question\" :\"What was the name of the first company you worked at?\",\"answer\" :\"******\"}";
What I am trying:
String regex = "(.*answer\"\\s:\"){1}(.*)(\"[\\s}]?)";
String rep = "$1*****$3";
System.out.println(test.replaceAll(regex, rep));
What I am getting:
"{ \"questionID\" :\"4\", \"question\":\"What is your favourite hobby?\",\"answer\" :\"answer 4\"},{ \"questionID\" :\"5\", \"question\" :\"What was the name of the first company you worked at?\",\"answer\" :\"******\"}";
Because of the greedy behaviour, the first group catches both "answer" parts, whereas I want it to stop after finding enough, perform replacement, and then keep looking further.
The pattern
("answer"\s*:\s*")(.*?)(")
Seems to do what you want. Here's the escaped version for Java:
(\"answer\"\\s*:\\s*\")(.*?)(\")
The key here is to use (.*?) to match the answer and not (.*). The latter matches as many characters as possible, the former will stop as soon as possible.
The above pattern won't work if there are double quotes in the answer. Here's a more complex version that will allow them:
("answer"\s*:\s*")((.*?)[^\\])?(")
You'll have to use $4 instead of $3 in the replacement pattern.
The following regex works for me :
regex = "(?<=answer\"\\s:\")(answer.*?)(?=\"})";
rep = "*****";
replaceALL(regex,rep);
The \ and " might be incorrectly escaped since I tested without java.
http://regexr.com?303mm

match a string of characters between tags:

I have the following strings:
<PAUL SAINT-KARL 1997-05-07>
<BOB DEAN 2001-05-07>
<GUY JEDDY 2007-05-07>
I want a java regex that would match this type of pattern "name and date" and then extract the name and date separately.
I able to match them separately with the following java regex:
1) (\d{4}-\d{2}-\d{2})>
2) <([ A-Z&#;0-9-]*+)
What I'm looking for is one regex that would identify the full text pattern as provided, and then extract the subsections, such as the actual name, and the date.
I'm looking to use Matcher.group() to retrieve the complete match from the target string.
Thanks
Try this:
"<([ A-Z&#;0-9-]*?) (\\d{4}-\\d{2}-\\d{2})>"
I changed the *+ to *? to make the * match lazily.

Categories