I am using regular expression extractor with Match Number 2. The problem is when I use negative Match number in can refer matchNr variable but in case its positive I can't. Any one have idea about this?
This is by design. If you have zero or positive number in Match No JMeter's Post-Processor will return not more than one match, that's why you won't have this xxx_matchNr JMeter Variable.
As per Regular Expression Extractor documentation (the approach is applicable to all Post-Procesors)
Match No. (0 for Random)
Indicates which match to use. The regular expression may match multiple times.
Use a value of zero to indicate JMeter should choose a match at random.
A positive number N means to select the nth match.
Negative numbers are used in conjunction with the ForEach Controller - see below.
More information: Using RegEx (Regular Expression Extractor) with JMeter
Related
I have the following RSpec output:
30 examples, 15 failures
I would like to subtract the second number from the first. I have this code:
def capture_passing_score(output)
captures = output.match(/^(?<total>\d+)\s*examples,\s*(?<failed>\d+)\s*failures$/)
captures[:total].to_i - captures[:failed].to_i
end
I am wondering if there is a way to do the calculation within a regular expression. Ideally, I'd avoid the second step in my code, and subtract the numbers within a regex. Performing mathematical operations may not be possible with Ruby's (or any) regex engine, but I couldn't find an answer either way. Is this possible?
Nope.
By every definition I have ever seen, Regular Expressions are about text processing. It is character based pattern matching. Numbers are a class of textual characters in Regex and do not represent their numerical values. While syntactic sugar may mask what is actually being done, you still need to convert the text to a numeric value to perform the subtraction.
WikiPedia
RubyDoc
If you know the format is going to remain consistent, you could do something like this:
output.scan(/\d+/).map(&:to_i).inject(:-)
It's not doing the subtraction via regex, but it does make it more concise.
I just learned from a book about regular expressions in the Ruby language. I did Google it, but still got confused about {x} and {x,y}.
The book says:
{x}→Match x occurrences of the preceding character.
{x,y}→Match at least x occurrences and at most y occurrences.
Can anyone explain this better, or provide some examples?
Sure, look at these examples:
http://rubular.com/r/sARHv0vf72
http://rubular.com/r/730Zo6rIls
/a{4}/
is the short version for:
/aaaa/
It says: Match exact 4 (consecutive) characters of 'a'.
where
/a{2,4}/
says: Match at least 2, and at most 4 characters of 'a'.
it will match
/aa/
/aaa/
/aaaa/
and it won't match
/a/
/aaaaa/
/xxx/
Limiting Repetition good online tutorial for this.
I highly recommend regexbuddy.com and very briefly, the regex below does what you refer to:
[0-9]{3}|\w{3}
The [ ] characters indicate that you must match a number between 0 and 9. It can be anything, but the [ ] is literal match. The { } with a 3 inside means match sets of 3 numbers between 0 and 9. The | is an or statement. The \w, is short hand for any word character and once again the {3} returns only sets of 3.
If you go to RegexPal.com you can enter the code above and test it. I used the following data to test the expression:
909 steve kinzey
and the expression matched the 909, the 'ste', the 'kin' and the 'zey'. It did not match the 've' because it is only 2 word characters long and a word character does not span white space so it could not carry over to the second word.
Interval Expressions
GNU awk refers to these as "interval expressions" in the Regexp Operators section of its manual. It explains the expressions as follows:
{n}
{n,}
{n,m}
One or two numbers inside braces denote an interval expression. If there is one number in the braces, the preceding regexp is repeated n times. If there are two numbers separated by a comma, the preceding regexp is repeated n to m times. If there is one number followed by a comma, then the preceding regexp is repeated at least n times:
The manual also includes these reference examples:
wh{3}y
Matches ‘whhhy’, but not ‘why’ or ‘whhhhy’.
wh{3,5}y
Matches ‘whhhy’, ‘whhhhy’, or ‘whhhhhy’, only.
wh{2,}y
Matches ‘whhy’ or ‘whhhy’, and so on.
See Also
Ruby's Regexp class.
Quantifiers section of Ruby's oniguruma engine.
How does backtracking differ from back-referencing in regular expressions?
How does back-referencing win limitation having with backtracking or vice-versa?
Backtracking is a way for a state machine to back up and retry other matches for a regular expression. It's something that's pretty much internal to the regex engine.
For example, say you're trying to match the regex [a-z]*a, any number of lower case characters followed by an a.
Given the input abca, a greedy match will assign all of that to the [a-z] portion of the regex but then there's no way to match the final a. Backtracking allows the engine to back up by returning that final a to the input stream and trying again, assigning abc to the [a-z] portion and a to the a portion.
Back-referencing on the other hand, is a means for a user of the regex engine to reference previously captured groups. For example,
s/^([a-z])([a-z])/\1_\2/
\_____/\_____/
| |
| +- capture group 2
+-------- capture group 1
may be a command to insert _ between two consecutive lower case letters at the start of each line. The \N back-reference (where N represents a number) refers back to the groups captured within ().
Can anybody help me write a regular expression which could find all the instances of the following in a long string >
type="array" count="x" total="y"
where x and y could be any numbers from 1 to 100.
language is ruby.
First, since we'll use the regex for a number twice, we'll save it as its own variable. Note that the number regex is comprised of three separate pieces: one-digit numbers, two-digit numbers, and three-digit numbers. This is a good rule of thumb to use when trying to make a regex to match a range of numbers. It's easy to get it wrong otherwise (allowing strings like "07").
Once you have the number regex, the rest is easy.
number = /[1-9]|[1-9][0-9]|100/
regex = /type="array" count="#{number}" total="#{number}"/
string.scan(regex)
This will return an array of matches
long_string.scan(/type="array" count="(?:[1-9]\d?|100)" total="(?:[1-9]\d?|100)")
The NSNumberFormatter class has plusSign and minusSign properties, but also has positivePrefix and negativePrefix.
What is the difference between these two? If I specify both, which one comes first?
plusSign and minusSign are used for the mathematical addition and subtraction operators. positivePrefix and suffix and negativePrefix and suffix are used to describe what characters/strings are used todisplay whether a certain numeric value is positive or negative.
To illustrate why they are different: most of the times, when a positive numeric value is displayed anywhere you'll just see numbers, No prefix or suffix. Negative numeric values have a minus in front, or behind them, or, in some styles of accounting they're just enclosed in brackets. Either way we'll still need a + and a - to express mathematical operations.