We use a library which uses the regular expression
Pattern.compile("^\\w+(\\.\\w+)*$")
which is used to validate a string .
For example abc.xyz is valid string and it passes through the validation.
As a workaround for another issue i need provide the string as abc.xyz,efg.ghi, which obviously does not get past the regex validation.Is there a way to make this string pass through the validation and if yes, how ?
PS: I tried using the escape sequences abc.xyz\\,efg\\.ghi. It did not work .
Just put comma and dot inside a character class.
Pattern.compile("^\\w+([,.]\\w+)*$");
DEMO
As it stands now, you can't pass in , characters. However, if there really is no way to change the library (e.g. proprietary), you can abuse Java's String cache + reflection to change the String literal before the proprietary class is loaded.
Related
I'm using Load Runner v11.52.
The right boundary is dynamically changing - I have used web_reg_save_param function but additional value is passed by the request during this process.
My exact situation is:
R̲e̲qu̲e̲s̲t̲ S̲t̲r̲i̲n̲g → R̲e̲s̲po̲n̲s̲e̲ S̲t̲r̲i̲n̲g
abcd1234*cat → abcd123428*cat
abcd2345*dog → abcd234532*dog
Here 1234 and 2345 are the dynamic value that I should capture but when I do so, it is passing 28 and 32 (which are not present in the request) and also dynamic that I have to capture that in another parameter.
The right boundary cat and dog which is changing dynamically should also be captured.
If anybody needs any more information I'm more than happy to provide.
Can anyone help me on this can anyone suggest a proper function for this issue.
Why not just use the boundaries before and after your complete string? This would result in a string similar to:
!leftboundary!abcd1234*cat!rightboundary!
Capture the entire string set and then take advantage of your foundation skills in C string processing skills to determine the location of the '*' separator, cutting your string into two distinct strings for correlation purposes.
I believe you can achieve what you're looking for by clicking the "Advanced" button when defining a rule, and specifying the "Length" parameter. This will instruct the rule to correlate just a substring of the what appears between the boundaries
Better make a use of regular expression.
for example:
If u want to fetch dynamic value - abcd123428*cat
use, web_reg_save_param_regex("Name=cPetId", "LB/dig=abcd([0-9]*)" , "RB=*cat" , LAST);
Loadrunner uses PCRE syntax for regular expression. The output with the below code will be cat and dog . Use this similarly for other dynamic values.
web_reg_save_param_regexp(
"ParamName=dynamicAnimal",
"RegExp=.*\*([a-z]*)",
SEARCH_FILTERS,
LAST);
Try to use lr_save_param_regexp function which allows you to use regular expressions.
I want to make a regex so I can do a "Search/Replace" over a json file with many object. Every object has a key named "resource" containing a URL.
Take a look at these examples:
"resource":"/designs/123/image.jpg"
"resource":"/designs/221/elephant.gif"
"resource":"/designs/icon.png"
I want to make a regex to replace the whole url with a string like this: localhost:8080/filepath.
This way, the result would be:
"resource":"localhost:8080/designs/123/image.jpg"
"resource":"localhost:8080/designs/221/elephant.gif"
"resource":"localhost:8080/designs/icon.png"
I'm just starting with regular expressions and I'm completely lost. I was thinking that one valid idea would be to write something starting with this pattern "resource":"
How could I write the regular expression?
The easiest method is probably just to replace "resource":"/ with "resource":"localhost:8080/. You don't even need a regex for this (but if you do you just have to escape some stuff).
With vim this would be
:%s/"resource":"\(.*\)"/"resource":"localhost:8080\1"
this should be easily transferable to java.
Good morning. I realize there are a ton of questions out there regarding replace and replaceAll() but i havnt seen this.
What im looking to do is parse a string (which contains valid html to a point) then after I see the second instance of <p> in the string i want to remove everything that starts with & and ends with ; until i see the next </p>
To do the second part I was hoping to use something along the lines of s.replaceAll("&*;","")
That doesnt work but hopefully it gets my point across that I am looking to replace anything that starts with & and ends with ;
You should probably leave the parsing to a DOM parser (see this question). I can almost guarantee you'll have to do this to find text within the <p> tags.
For the replacement logic, String.replaceAll uses regular expressions, which can do the matching you want.
The "wildcard" in regular expressions that you want is the .* expression. Using your example:
String ampStr = "This &escape;String";
String removed = ampStr.replaceAll("&.*;", "");
System.out.println(removed);
This outputs This String. This is because the . represents any character, and the * means "this character 0 or more times." So .* basically means "any number of characters." However, feeding it:
"This &escape;String &anotherescape;Extended"
will probably not do what you want, and it will output This Extended. To fix this, you specify exactly what you want to look for instead of the . character. This is done using [^;], which means "any character that's not a semicolon:
String removed = ampStr.replaceAll("&[^;]*;", "");
This has performance benefits over &.*?; for non-matching strings, so I highly recommend using this version, especially since not all HTML files will contain a &abc; token and the &.*?; version can have huge performance bottle-necks as a result.
The expression you want is:
s.replaceAll("&.*?;","");
But do you really want to be parsing HTML this way? You may be better off using an XML parser.
I need a simple way to implement the contains function using matches. I believe this is my starting point:
xxx.matches("'.*yyy.*'");
But I need to make it a universal method and pre-process whatever I search for to be accepted by matches! This must be done using only the escape '\' character!
Imagine a string SEARCH_FOR that can contain some special characters that must be "regex escaped"...
String SEARCH_FOR="*.\\"
xxx.matches("'.*" + SEARCH_FOR + ".*'");
Are there any catches? Special situations? Any other "special chars should be taken into account?
Are you looking for Pattern.quote(String) ?
This escapes special characters for you.
EDIT:
After reading the comments, I really hope you try Pattern.quote(yourString.toLowerCase()) as it sounds like you've been using Pattern.quote(yourString).toLowerCase(). If DataNucleus is applying the regex then there should be no problems with using the \Q and \E escape sequence.
Since you have really asked for it, ".\\".replaceAll("(\\.|\\$|\\+|\\*|\\\\)", "\\\\\$1") outputs \.\\
This will escape .'s, $'s, + 's, *'s and \'s. Note that the security of this is now all upon you. If you don't escape something you needed to, or you escape it incorrectly, you will either allow people to use regex inside the search term when you weren't expecting to or it won't returns results that you were expecting.
How can I replace String in xml ..
I've
<schema>src/main/castor/document.xsd</schema>
I need to replace to
<schema>cs/src/main/castor/document.xsd</schema>
If I use simple , xmlInStr is the string form of xml document
xmlInStr.replaceAll(
"src/main/castor/GridDocument.xsd",
"correspondenceCastor/src/main/castor/GridDocument.xsd"
);
I Tried replace instead ,
xmlInStr.replace("src/main/castor/GridDocument.xsd".toCharArray().toString(), "correspondenceCastor/src/main/castor/GridDocument.xsd".toCharArray().toString());
it's not working . any clues
Managed like this
int indx = from.indexOf(from);
xmlInStr = xmlInStr.substring(0,indx) + to + xmlInStr.substring(indx + from.length());
String.replaceAll takes a regular expression as the first argument. Use replace instead.
You use an XML parser to parse and manipulate XML, don't try and use regular expression based string replacement mechanisms it will not work and will only bring pain and suffering.
You can use repalce or replaceAll. Anyway you have to use the value returned by this method. The method does not modify the string itself because String class is immutable.
Both replace() and replaceAll() don't actually replace anything in the string (strings are immutable). They return a new string instead, but you just discard the return value, that's why you don't see it anywhere. By the way, that .toCharArray().toString() looks completely useless to me. A character literal is already a full-fledged String.
But you really should use an XML parser instead. Unless your task is very simple and you're absolutely sure that you don't replace anything that shouldn't be replaced.