Regular expression - How to exclude certain string from the results set? - regex

I am pretty new in Regular Expression.
Input
/shop/earrings
/shop/yellow-gold-earrings
/shop/white-gold-earrings
/shop/rose-gold-earrings
/shop/necklaces
/shop/yellow-gold-necklaces
/shop/white-gold-necklaces
/shop/rose-gold-necklaces
/shop/best-buy-earrings
Regular Expression I used
\/shop\/[a-z-]*-?earrings
Desired Result
/shop/earrings
/shop/yellow-gold-earrings
/shop/white-gold-earrings
/shop/rose-gold-earrings
Actual Result
/shop/earrings
/shop/yellow-gold-earrings
/shop/white-gold-earrings
/shop/rose-gold-earrings
/shop/best-buy-earrings
I do not want /shop/best-buy-earrings to be in the result. Please help me to fix the Regular Expression. Thank you.

Simply add gold to the regex before the - and surround with parenthesis:
\/shop\/([a-z-]*gold-)?earrings

Assuming PCRE flavour, you can use:
\/shop\/(?!best-buy)(?:\w+-)*earrings
Or, if you can use other delimiter than slash:
/shop/(?!best-buy)(?:\w+-)*earrings
Demo

Related

Regular Expression Pattern which should not allow ,;:|

I need the regular expression pattern which should not allow to put any of following characters into input in HTML
,;:|
You may get some idea from this.
[^,;:|]+
DEMO::: https://rubular.com/r/dKQzC1HrnMG88X

In JMeter I need to extract a specific Regular Expression

In the following String:
Events('1234', '123456', '', 'QW233Cdse');
I need to extract "QW233Cdse"
Any suggestion?
When we are working with regular expressions then its very important that we should look for the static text in the test string that can help to create a strong regular expression.
As in your case, "Events()" seems to be a static text containing dynamic value in the round parenthesis so in order to generate the regular expression you need to keep 'Events()' text and add the expression in the round parenthesis as mentioned below:
Test String: Events('1234', '123456', '', 'QW233Cdse');
Regular Expression can be:
Events(.'(.)');
Events(.* '(.+?)');
Note: The backslash before round parentheses would avoid interpreting the round braces as unescaped character. For example, a parenthesis "(" begins the definition of a quantifier, but the leading backslash of parenthesis "(" indicates that the regular expression should match the parenthesis.
Regular expression is most important item to learn when you are working with load testing tools and you can refer to below blog post to get more information on regular expression:
https://www.redline13.com/blog/2016/01/jmeter-extract-and-re-use-as-variable/
Let me know if you have any further question
The relevant regular expression would be something like:
Events\(.* '(.+?)'\);
Demo:
References:
JMeter: Regular Expressions
Using Regular Expressions in JMeter
Perl 5 Regex Cheat sheet
Try using this regex:
\w+(?='\))
Regex would be:
, '([^']+?)');
Configuration would be:

Need a regular expression to extract the value in "Signature" from this string

I need to extract the value in "Signature" from the below string.
What is the regular expression to use for this?
{"payload":"eyJjaGFsbGVuZ2UiOiIwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAiLCJtaW5pbXVtTW9iaWxlVmVyc2lvbiI6MSwibm9uY2UiOiJPQ1dwdjkzSzRMSCtQM0FOQmJDM0NCcHRQMmtES1M5TFZPVGFpWU9wUXFFPSJ9","signature":"hlMKmwJt8f24jv6MluM0W+0Kzxo96XlfM1GDop3CHPgLnLFpLmJ9imHFeCCU5bedA4jREuuQ5Y9pY+AqU5qt5QHAjPrtFyT69PktV8B7e+i9tqXae8haIB5iQUD9ln2yjMPE3XfG8+4jHEnHv7x7dePKr4zRgRTRQj6ZbMBaFGt1nhrDexfwHTiGfwuMGuneHUsBeb/EHjaKyGcOoLNq1xjHijlYVb3C3RF+y04VraN3Pt1mfjG9O+3Jvt3cF1xL4nhQO93ZvZ6UU2a0x74PutXxhfoWaeMiZSG0nCBGVP8WNDil1V00cX1Darpp/Xti6RlJJO+CKESInoBMxIWWYw=="}
Please see the following regex:
/(?<="signature":")[^"]+/
View a regex demo!
It should be:
,"signature":"([^"]+?)"
But you should definitely learn regular expression syntax.
To get the signature string use:
"signature":"(.*?)"
The string is in Group 1 of the regex.
Here the signature string is in Group 0 of the regex.
(?<="signature":").*?(?=")

Regular expression for format XX-XXXX

I want a regular expression for format xx-xxxx can somebody help? It will take numbers and
characters. I have no idea about regular expressions. also can some explain that how whole expression works?
Thanks
Assuming that the string will be only containing exactly that string with no leading or trailing whitespace:
/^[a-zA-Z0-9]{2}-[a-zA-Z0-9]{4}$/
If you need to find that pattern within another string (a paragraph?) you would use:
/\b[a-zA-Z0-9]{2}-[a-zA-Z0-9]{4}\b/
The expression:
\A[0-9a-zA-Z]{2}-[0-9a-zA-Z]{4}\Z
will also work.

Use a character to look ahead, but only when it exists

Suppose I have these line:
/path-to-something/section/resource?var=name
/path-to-something/section/resource
I want to use regular expression to capture the text between /path-to-something/ and the ? sign. So for both cases, I want the output to be:
section/resource
The farthest I can go is to use this regex:
(?<=/path-to-something/).+(?=\?)
But it fails for the second case (where the URL doesn't have the ? sign):
section/resource
[no match]
Is there a way to do something like this in Regular Expression? I know I can do this without regular expression, but I wanted to know if this is possible to do in regex.
How about:
(?<=/path-to-something/)[^?]+