I have the following XML String:
<asd1:content></asd1:content>
The namespace prefix asd1 could be different at different places in the XML file.
I want to modify it to :
<asd1:content>*</asd1:content>
I am trying to do it via regex as follows:
myString.replaceAll("<.*:content></.*:content>","replacement text");
The problem is that I don,t want to lose the namespace prefix. What should I do?
Please note that you've 2 typos:
cotent instead of content
replaceAlll instead of replaceAll
If you still need a regex, you can use:
String resultString = subjectString.replaceAll("(?ism)<(.*?):content></(.*?)\\.content>", "<$1:content>*</$2.content>");
Related
In JMeter, I used a Regular Expression Extractor to extract part of an HTML response. I then passed that to a BeanShell Post Processor. However, having trouble replacing \x2D to -. Is there a way to do this or perhaps do I need to extract the response as
String yourvar = vars.get("accessToken");
String anotherVar = yourvar.replace("data.access_token = '","");
String finalAccessToken = anotherVar.replace("\x2D","-");
vars.put("finalAccessToken",finalAccessToken);
It is not liking the "\x2D" part. It works if I find \x2D but the original string only has .
You need to escape your target String parameter.
final String finalAccessToken = anotherVar.replace("\\x2D", "-");
If it's not what you're asking for, add more info to the question. That's all what I was able to understand.
It is recommended to use JMeter's built-in test elements where possible. In particular your case you might be interested in __strReplace() custom JMeter Function
Install Custom JMeter Functions bundle using JMeter Plugins Manager
Use the following expression to make the replacement:
${__strReplace(${anotherVar},\\\x2D,-,)}
If you want to go for scripting - make sure to use JSR223 PostProcessor and Groovy language. Be aware that you will still need to escape backslash with another backslash like:
String finalAccessToken = anotherVar.replace("\\x2D","-");
I have a part of HTML from a website in the below String format:
srcset=" /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#200w.jpg?20170808 200w, /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#338w.jpg?20170808 338w, /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#445w.jpg?20170808 445w, tesla_theme/assets/img/homepage/mobile/homepage-models--touch#542w.jpg?20170808 542w, /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#750w.jpg?20170808 750w"
I want to add http://tesla.com in front of all the urls in the srcset element like http://tesla_theme/assets/img/homepage/mobile/homepage-models--touch#750w.jpg?20170808 750w
I believe this could be done using regex, but I am not sure.
How do I do this using Java if I have multiple srcset elements in a html string variable, and I want to replace all of the srcset url.'s and add the server url in front?
Note: The /tesla_theme will not be consistent, so I cannot use replaceAll, instead, i will have to use regex.
You can simply use String Class replace method as below, It will replace all "/_tesla" in the given String. No special regex required unless you have a kind of pattern instead of "/tesla"
String srcset=" /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#200w.jpg?20170808 200w, /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#338w.jpg?20170808 338w, /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#445w.jpg?20170808 445w, tesla_theme/assets/img/homepage/mobile/homepage-models--touch#542w.jpg?20170808 542w, /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#750w.jpg?20170808 750w";
String requiredSrcSet = srcset.replace("/tesla_", "http://tesla_");
What's the best way to replace a path url placeholder. I have the following that needs to be replaced
/user/:name/password/:password
as
/user/{name}/password/{password}
Is there a library that could do this for me in Java?
Since the format is so simple, and : isn't a valid URL character, I would just use a basic regex matching : followed by any word, capturing the word for reprint.
"/user/:name/password/:password".replaceAll(":(\\w+)","{$1}")
Just using String#replaceAll can achieve your way.
"/user/:name/password/:password".replaceAll(":(\\w+)","{$1}")
Did you try to use replaceAll like this :
String str = "/user/:name/password/:password";
String result = str.replaceAll(":(\\w+)", "{$1}");
Output
/user/{name}/password/{password}
Using Java, String and replaceAll I have to replace values of elements that may come with different name spaces:
from
<tns:p>to be replaced</tns:p>
<sss:p>to be replaced</sss:p>
to
<tns:p>replaced</tns:p>
<sss:p>replaced</sss:p>
Could you please, help to find regular expression for this replace?
P.S. The elements may appear more than once in a given string:
<tns:p>to be replaced</tns:p>
<tns:w>not to be replaced</tns:w>
<tns:p>to be replaced</tns:p>
I have a problem with variable name spaces in front of elements.
Without them I'd do like this:
str.replaceAll("(?<=<p>)(.*?)(?=</p)", "replacement")
The problem is that look-behinds can't have variable lengths, but if your input is well formed (that is tags are closed with matching tags), and you text to be replaced isn't a CDATA element that itself contains the closing tag (seems unlikely), this will work:
str = str.replaceAll("(?<=[:<]p>)[^<]*(?=</(\\w+:)?p>)", "replacement");
This regex makes the replacement whether or not there's a namespace.
Here's some test code:
String str = "<p>to be replaced</p><tns:p>to be replaced</tns:p><tns:w>not to be replaced</tns:w><tns:p>to be replaced</tns:p>";
str = str.replaceAll("(?<=[:<]p>)[^<]*(?=</(\\w+:)?p>)", "replacement");
System.out.println(str);
Output:
replacementreplacementnot to be replacedreplacement
If you input is not well formed and simple, ie the closing tag namespace may not be the same, you can do it by capturing the namespace, using a back-reference to assert it's the same in the closing tag, and putting it back in the replacement:
str = str.replaceAll("(<(\\w+:)?p>)[^<]*(?=</(\\2)p>)", "$1replacement");
The namespace is still optional, but now the namespace in the closing tag must match that of the opening tag.
Lookbehinds in java regex don't support repitive operators, So unfortunatly this is not possible with just one String#replaceAll(String, String)
I have a XML for which i am writing a servlet to pick up contents from the XML. One such tag is <itunes:author>Jonathan Kendrick</itunes:author>
I need to get author value for this. because of :
I tried using namespace and using escape sequence for : but it did not worked for me.
For rest of other XML elements i am simply using
String link=node.getChildText("link").toString();
I am using Jdom parser
in your XML the sequernce 'itunes:author' represents what's called a Q-Namem a "Qualified Name". In XML it consists of a 'Namespace prefix', and a 'Local Name'. In your example, the namespace prefix is 'itunes', and the 'local name' is 'author'.
What you want is the 'author' element in the namespace linked to the prefix 'itunes'. The actual namespace is normally a full URL. I believe the full URL for your example is probably xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd", but you should check that.
So, the Namespace is "http://www.itunes.com/dtds/podcast-1.0.dtd", it's prefix is declared to be 'itunes' (but it could be something else - the actual prefix name is not technically important...)
You want to get the 'author' in the 'http://www.itunes.com/dtds/podcast-1.0.dtd' Namespace so you want:
String author = node.getChildText("author", Namespace.getNamespace("http://www.itunes.com/dtds/podcast-1.0.dtd"));
For more information on Namespaces check out: http://www.w3schools.com/xml/xml_namespaces.asp