How to get method/function in a string by using regex - regex

I trying to get arguments from function in the string.
Argument possible to contains:
Example
Placeholder:
{{request.expires_in}}
//can match regex: \{\{[a-zA-z0-9\.\-\_]\}\}
function
#func_compare('string1','string2',1234)
Others:
dERzUxOfnj49g/kCGLR3vhzBOTLwEMgrpa1/MCBpXQR2NIFV1yjraGVZLkujG63J0joj+TvNocjpJSQq2TpPRzLfCSZADcjmbkBkphIpsT8=
//Any string except brackets
Case
Below is the sample case I working with.
Content:
#func_compare('string1',#func_test(2),1234),'Y-m-d H:i:s',#func_strtotime({{request.expires_in}} - 300)
Regex using:
(?<=#func_compare\().*[^\(](?=\))
I expect will get
'string1',#func_test(2),1234
But what matched from the regex now is
'string1',#func_test(2),1234),'Y-m-d H:i:s',#func_strtotime({{request.expires_in}} - 300
Anyone know how to get the arguments in between the #func_strtotime brackets. I will appreciate any response.

Would you please try:
(?<=#func_compare\().*?(?:\(.*?\).*?)?(?=\))
which will work for both cases.
[Explanation of the regex]
.*?(?:\(.*?\).*?)?(?=\))
.*? the shortest match not to overrun the next pattern
(?:\(.*?\).*?)? a group of substring which includes a pair of parens followed by another substring of length 0 or longer
(?=\)) positive lookahead for the right paren

You'll get the result using recursive regex:
(?<=#func_compare\()([^()]*\((?:.*?\)|(?1))*)[^()]*(?=\))
Demo & explanation

Related

Regex to find after particular word inside a string

I am using regex to find few keywords after colon(:) and the best I have reached so far is:
sample test case
test {
test1 {
sadffd(test: "aff", aaa: "aa1") {}
}
}
Now I have to find a keyword inside () brackets and its working for 'aaa' but when I add test it fails, it matches entire words in string.
my regex so far
\btest(.*\w") (failed case) expected "aff" returned "aff", aaa: "aa1"
\baaa(.*\w") (pass case) returned "aa1"
please let me know if more information is needed
You may try
:\s*"(.*?)"
And the data you need is in the first capturing group.
Explanation
:\s*"(.*?)"
: colon
\s* followed by optionally any number of spaces
" followed by quote
( ) capturing group, containing...
.*? any number of character, matching as few as possible
" followed by quote
Demo:
https://regex101.com/r/WnvzdG/1
Update:
If you want to match ONLY after specific keywords, followed by colon, you can do something like:
(KEYWORD1|KEYWORD2|KEYWORD3)\s*:\s*"(.*?)"
First capture group will be the keyword matched, second capture group will be the value.
One more approach (executed in Python)
items = ['test{test1 {sadffd(test: "aff", aaa: "aa1") {}}}']
for item in items:
print(re.findall(r'"(\w+)"',item))
print(re.findall(r'(?<=: )"(\w+)"',item))
Output
['aff', 'aa1']
['aff', 'aa1']
I believe a simple regex would work to get everything inside the double quotes in your case:
("\w+")
Note that your question above says you want to capture "aff" and not just aff so I've included the surrounding quotes within the capturing group.
Example from regex101:
It's pretty crude but this should be OK for the input you've presented. (It wouldn't handle things like an escaped double quote in the string, for example).

Remove string between 2 pattern using gawk regex only

Input:
secNm:ATA,_class:com.dddao.domaffin.summaggrfy.GddenericMohsg},{ttlRec:0,ttlVal:{:0}secNm:B2B,_class:com.xyz.dakjdain.sfffummary.GenericMo73hs}extra
secNm:ATA,_class:com.dddao.domaffin.summaggrfy.GddenericMohsg},{ttlRec:0,ttlVal:{:0}secNm:B2B,_class:com.xyz.dakjdain.sfffummary.GenericMo73hs
In above both the string I want to remove,
For String 1: parts which stars from ",_class" and ends at 1st occurrence "}"
For String 2: parts which stars from ",_class" till the end if if 1st condition fails.
Output:
secNm:ATA,{ttlRec:0,ttlVal:{:0}secNm:B2Bextra
secNm:ATA,{ttlRec:0,ttlVal:{:0}secNm:B2B
This type of pattern is present undefinable times in this above string.
I want simple want to remove those part.
I have written regex function gsub(/,_class(.*?)\}/,"",$0)
I want answer only using gawk regex function only no other method.
My above give function is having some issue and removing big part of the string.
Help me to correct my regex formula please.
Thanks in advance.
You may use a [^}] negated bracket expression to match any char but } since lazy quantifiers are not supported.
Besides, you do not even need a grouping construct here as you are not referring to the captured value here. You may remove ( and ) safely.
Use
/,_class[^}]*}/
Basically, this should be understood as:
,_class - match ,_class substring
[^}]* - 0 or more chars other than }
} - up to and including }.

Ignoring a specific pattern (Regex)

Text:
Total amount due (inc. GST $7.68) $84.55
Regex:
Total Amount.?\s*(?!\(inc\))?[^\$]*(\$\s?[0-9,]+(\.[0-9]{1,})?)
https://regex101.com/r/YXr023/1
but it matches the first amount that it finds $7.68. How do I ignore anything inside brackets so it matches $84.55 instead?
Appending (?![^\(]*\)) to the end ignored the line completely, which I don't want.
Hope this post will help you out..
Regex: (?<!\()\$[\d\.]+(?!.*?\))
1. (?<!\() negative look behind for (
2. \$[\d\.]+ match strings like $10.1000
3. (?!.*?\)) does not contain any further )
Regex Demo

Not able to Create regular expression for a Voucher number generated in this format "484,0116/BRD/0000267" and I have to use only 0116/BRD/0000267

I have tried many combinations to extract 0116/BRD/0000267 from this number "484,0116/KMO/0000267" but not able to extract, it is showing ERROR - jmeter.extractor.RegexExtractor: Error in pattern: [^,](*[0-9]|/|*[A-Z]|/|*[0-9]+?)"
Please help if anybody have answer for this situation.
Thanks in advance. Images
Response from web page
setting of regular expression
I think it should be enouth to solve your problem.
^\d+,(\d+\/\w+\/\d+)
For a better explanation:
https://regex101.com/r/oI0nP6/1
The main problem with this regex is that you inserted pipes (alternation operator in regex) where you really did not intend to use alternation, but continuation. The * quantifier cannot be applied to the alternation operator.
Use
[0-9]*/[A-Z]*/[0-9]+
or (if the substring is always at the end of the string):
[0-9]*/[A-Z]*/[0-9]+$
See regex demo
Explanation:
[0-9]* - matches 0 or more digits (perhaps, * can be replaced with + to match 1 or more occurrences)
/ - a literal forward slash
[A-Z]* - 0 or more uppercase ASCII letters (again, perhaps, * can be replaced with + to match 1 or more occurrences)
/ - a literal forward slash
[0-9]+ - 1 or more digits.
The $ asserts the position at the end of the string.
That should work with $0$ variable. You can also make your fixed pattern work with
[^,]*,([0-9]*/[A-Z]*/[0-9]+)
Use it with $1$. If the string pattern is always digits+,+digits+/+uppercase letters+/+digits, you can just use
^\d+,(\d+/[A-Z]+/\d+)$
Again, with $1$.
After very long try I have got the answer. Thank you guys for your support. The expression to extract 0116/BRD/0000267 from "484,0116/BRD/0000267" is ,(.+)?\"
This worked for me in Jmeter. Thank you all of you for supporting me., = was bcas I want string after this (.+) = Was for the whole string and numbers ?= was to stop when I got result \" = was to stop before " (inverted comma)
I have used template $1$ and field check in = Body. Image is here for my regex setting. Regex expression setting for jmeter
Thank you :)
We have shared this details on
http://www.knowledgeworldforyou.com/?p=276

Using regex to find a pattern which does not start with a certain String

I need to regex-match numbers in a certain pattern which works already, but only if there is not (+ right in front of it.
Example Strings I want to have a valid match within: 12, 12.5, 200/300, 200/300/400%, 1/2/3/4/5/6/7
Example Strings I want to have no valid match within: (+10% juice), (+4)
I can already get all the valid matches with (\d+[/%.]?)+, but I need help to exclude the example Strings I want to have no valid match in (which means, only match if there is NOT the String (+ right in front of the mentioned pattern).
Can someone help me out? I have already experimented with the ! (like ?!(\(\+)(\d+[/%.]?)+) but for some reason I can't it to work the way I need it.
(You can use http://gskinner.com/RegExr/ to test regex live)
EDIT: I did maybe use wrong words. I don't want to check if the searchstring does start with (+ but I want to make sure that there is no (+ right in front of my String.
Try regexr with the following inputs:
Match: (\d+[/%.]?)+
Check the checkbox for global (to search for more than one match within the text)
Text:
this should find a match: 200/300/400
this shouldnt find any match at all: (+100%)
this should find a match: 40/50/60%
this should find a match: 175
Currently it will find a match in all 4 lines. I want a regex that does no longer find a match in line 2.
The regex construct you are wanting is "Negative Lookbehind" - See http://www.regular-expressions.info/lookaround.html. A negative lookbehind is defined like (?<!DONTMATCHME) where DONTMATCHME is the expression you don't want to find just before the next bit of the expression. Crutially, the lookbehind bit is not considered part of the match itself
Your expression should be:
(?<![+\d/\.])(\d+[/%.]?)+
Edit - changed negative lookbehind to any character that is not a + or another digit!
Edit #2 - moved the lookbehind outside the main capture brackets. Expanded the range of not acceptable characters before the match to include / & .