Dynamic right boundary in Load Runner 11.52 - java

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.

Related

Regex to dynamically replace text in java

What I am trying to do is make something like:
some random stuff substitute("random stuff","xxx") test
be replaced to the following:
some xxx test
If I use:
substitute\((.*?)*\)
I get to find the portion, but what I ultimately want is multiple groups where the first group is the text to search and the second group is to replace. I want it to be generic enough so I dont depend on the , since it can appear anywhere. Is there a regex that could work for all cases or should I be depending on the "" to get what I need?
Don't have tested it but you could try
\w*\(\"(.*)\".*\"(.*)\"\)
If order in substitute doesn't change you have in your first group the search and in second the replace term.
And yes I don't see another way but depending on () and "".

How to bypass reqular expression validation for specific characters

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.

What's the best way to parse this kind of input?

I need to evaluate some strings (items titles) in order to decide if the item matches or not with some rules.
Regarding the rules, think about something like this.
((ball | balls) & messi) -ronaldo
The meaning is, if the title contains either ball or balls and messi (but not ronaldo) means that match.
What do you think is the best way to achieve this? (the rule must be written by end users and with a clear syntax, no regex)
Thanks!
You may try something that uses plain English and yet follows the rules of logic.
For example -
(("ball" OR "balls") AND ("messi")) AND NOT ("ronaldo")
Essentially, you will need your own Logic Evaluator to parse this expression and evaluate it to true or false. You may also have another program that works on such rules supplied by the user and converts them to a format that is more friendly to your Logic Evaluator.
I found this library Boolean Expressions and just built a wrapper to parse the keywords and works like a charm !!

Elegant way to do variable substitution in a java string

Pretty simple question and my brain is frozen today so I can't think of an elegant solution where I know one exists.
I have a formula which is passed to me in the form "A+B"
I also have a mapping of the formula variables to their "readable names".
Finally, I have a formula parser which will calculate the value of the formula, but only if its passed with the readable names for the variables.
For example, as an input I get
String formula = "A+B"
String readableA = "foovar1"
String readableB = "foovar2"
and I want my output to be "foovar1+foovar2"
The problem with a simple find and replace is that it can be easily be broken because we have no guarantees on what the 'readable' names are. Lets say I take my example again with different parameters
String formula = "A+B"
String readableA = "foovarBad1"
String readableB = "foovarAngry2"
If I do a simple find and replace in a loop, I'll end up replacing the capital A's and B's in the readable names I have already replaced.
This looks like an approximate solution but I don't have brackets around my variables
How to replace a set of tokens in a Java String?
That link you provided is an excellent source since matching using patterns is the way to go. The basic idea here is first get the tokens using a matcher. After this you will have Operators and Operands
Then, do the replacement individually on each Operand.
Finally, put them back together using the Operators.
A somewhat tedious solution would be to scan for all occurences of A and B and note their indexes in the string, and then use StringBuilder.replace(int start, int end, String str) method. (in naive form this would not be very efficient though, approaching smth like square complexity, or more precisely "number of variables" * "number of possible replacements")
If you know all of your operators, you could do split on them (like on "+") and then replace individual "A" and "B" (you'd have to do trimming whitespace chars first of course) in an array or ArrayList.
A simple way to do it is
String foumula = "A+B".replaceAll("\\bA\\b", readableA)
.replaceAll("\\bB\\b", readableB);
Your approach does not work fine that way
Formulas (mathematic Expressions) should be parsed into an expression structure (eg. expression tree).
Such that you have later Operand Nodes and Operator nodes.
Later this expression will be evaluated traversing the tree and considering the mathematical priority rules.
I recommend reading more on Expression parsing.
Matching Only
If you don't have to evaluate the expression after doing the substitution, you might be able to use a regex. Something like (\b\p{Alpha}\p{Alnum}*\b)
or the java string "(\\b\\p{Alpha}\\p{Alnum}*\\b)"
Then use find() over and over to find all the variables and store their locations.
Finally, go through the locations and build up a new string from the old one with the variable bits replaced.
Not that It will not do much checking that the supplied expression is reasonable. For example, it wouldn't mind at all if you gave it )A 2 B( and would just replace the A and B (like )XXX 2 XXX(). I don't know if that matters.
This is similar to the link you supplied in your question except you need a different regular expression than they used. You can go to http://www.regexplanet.com/advanced/java/index.html to play with regular expressions and figure out one that will work. I used it with the one I suggested and it finds what it needs in A+B and A + (C* D ) just fine.
Parsing
You parse the expression using one of the available parser generators (Antlr or Sable or ...) or find an algebraic expression parser available as open source and use it. (You would have to search the web to find those, I haven't used one but suspect they exist.)
Then you use the parser to generate a parsed form of the expression, replace the variables and reconstitute the string form with the new variables.
This one might work better but the amount of effort depends on whether you can find existing code to use.
It also depends on whether you need to validate the expression is valid according to the normal rules. This method will not accept invalid expressions, most likely.

Regex for optional leading forward slashes

I need to validate shipping container numbers. There is an industry standard that says only alpha-numeric and 11 characters in length is acceptable. eg: FBXU8891735
However there is also a standard industry practice where the first 4 characters can be forward-slashes eg: ////8891735
I have 2 requirements - firstly to validate the container numbers (eg. matches()) and secondly to clean the container numbers (eg. replaceAll())
System.out.println("MSCU3720090".matches("[a-zA-Z0-9]{11}")); //true - ok
System.out.println("////3720090".matches("[a-zA-Z0-9]{11}")); //false - fail
System.out.println("MSCU3720090".replaceAll("[^a-zA-Z0-9]*", "")); //MSCU3720090 - ok
System.out.println("////3720090".replaceAll("[^a-zA-Z0-9]*", "")); //3720090 - fail
I know that for matches() I can use an alternate eg:
[a-zA-Z0-9]{11}|////[a-zA-Z0-9]{7}
However this seems ugly and I'm not sure how to use it for replaceAll().
Can someone suggest a better regex to satisfy both requirements (or one for each requirement)?
Thanks.
"((?:[a-zA-Z0-9]{4}|/{4})[a-zA-Z0-9]{7})"
Then just examine the contents of capture group 1 for the number.
In case someone wants a proper validation of Cargo Container Number ISO 6346, please refer my Javascript class for the purpose or Patrik Storm's PHP Class.

Categories