How to use regex expression in Katalon Studio? - regex

I have a regex expression which checks for the valid date and timezone format but when I tried to use it in Katalon, I get unexpected errors about using the slases:
^\d\d([-/])\d\d\1\d{4} \d\d:\d\d [AP]M ET$
Here's an example of the regex that seems to be working in here: https://regex101.com/r/nZC9PB/1

The regex expression by user should work well in some languages / platform, but in order to make it accurate to Katalon, we should use escaped character '' before '/' in '[-/]'. So that Katalon Studio can understand the regex expression correctly.
The correct regex expression should be:
^\d\d([-\/])\d\d\1\d{4} \d\d:\d\d [AP]M ET$
We hope this helps. If you have any other questions when using Katalon platform, feel free to ask our global community of testers, developers, and QA/QC engineers on Katalon Community!

For doublequoted string in groovy you have to escape every \ and $ --> \\ and \$
But there is a slashy string in groovy where you need to escape only slash symbol / --> \/
https://groovy-lang.org/syntax.html#_slashy_string
def re = /^\d\d([-\/])\d\d\1\d{4} \d\d:\d\d [AP]M ET$/
assert "06-01-2023 11:59 PM ET" =~ re
"Ok"

Related

How can I search for a string inside double quotes?

I want to search for regex by separating each word and the string of double quotes.
For example, if I have a sentence like this
cmd -c \"reg query \\\"HKLM\\System\\CurrentSet\\\"\"
I should search by these groups.
[(cmd), (-c), (\"reg query \\\"HKLM\\System\\CurrentSet\\\"\")]
So I tried to make it like this with regular expression.
(([\'\"]).+?.*\")\s|(["A-Z\\a-z-]{0,})
But the search result came out like this.
[(cmd), (-c), (\"reg), (query), (\\\"HKLM\\System\\CurrentSet\\\"\")]
So I wonder how to write regular expressions to get these results.
[(cmd), (-c), (\"reg query \\\"HKLM\\System\\CurrentSet\\\"\")]
What about this regex: \\".*?(?<!\\\\)\\"|\S+
Explanation
\\".*? matches \" followed by anything with the ungreedy search.
(?<!\\\\)\\" matches \" which is not preceded by \\ (two slashes).
|\S+ means "or some non-space chars once or more".
Code
The PCRE version with PHP:
$regex = '/\\\\".*?(?<!\\\\\\\\)\\\\"|\S+/m';
PHP test here: https://regex101.com/r/uEUn6d/1
The Python version:
import re
regex = r"\\\".*?(?<!\\\\)\\\"|\S+"
Python test here: https://regex101.com/r/uEUn6d/3

Sublime text regex find/replace not recognising regex

I have to find/replace A LOT of inputs in a lot of files that have name and are either required or have minlength/maxlength or ng-pattern attribute.
I made this regex:
<input([^\/\>]*?) name="([\S\_]+?)" ([^\/\>]*?)(required|pattern|minlength|maxlength)([^\/\>]*?)\/\>
I tested this regex on regex101.com (as a js regex though) and it works.
Sublime probably has slightly different regex syntax than javascript, but I've been using regex find/replace a lot recently, and this is the first time it's not working.
I'm pretty sure it's these parts that are problematic:
([^\/\>]*?)
Any help would be appreciated.
< and > don't need to be escaped. So don't escape your the last character i.e change \> to >
<input([^\/\>]*?) name="([\S\_]+?)" ([^\/\>]*?)
(required|pattern|minlength|maxlength)([^\/\>]*?)\/>
^ Don't escape
instead of
<input([^\/\>]*?) name="([\S\_]+?)" ([^\/\>]*?)
(required|pattern|minlength|maxlength)([^\/\>]*?)\/\>
^ Doesn't WORK in sublimetext

Can't get REGEX to work ((?=(<\/))(.*?)(?:\>))

I have the following regex and it finds a partial solution for me:
((?=(<\/))(.*?)(?:\>))
Given the Following line:
</EventSubType><OrgUnitNm>###</OrgUnitNm></test:CommonAttributes><ProductArrangementBasic><ProductId>####</ProductId></part:Asking>
The regex will give me:
</EventSubType></OrgUnitNm></test:CommonAttributes></ProductId></part:Asking>
I want just:
</test: </part:
Any help would be great.
Thank you.
NOTE: if you are parsing HTML, you'd better use a dedicated library for that.
I suspect you are using the regex with PCRE /U tag, like /((?=(<\/))(.*?)(?:\>))/U.
If you want to obtain just </test: and </part:, you may use a much simpler regex:
/<\/\w+:/
See the regex demo
Details:
< - a literal <
\/ - a literal /
\w+ - 1 or more chars from [a-zA-Z0-9_] ranges
: - a literal :.

regex_error. What's wrong with my regex

I'm getting regex_error for some reason. I also tried it the regular way of using escape characters (this method eliminates the need for escape sequences in c++ 11 by putting R"(something)" )
By the way if anyone was wondering, they are for recognizing lines in xml
When I use a web based regex tester it works fine.
string sstart = R"(\w*+(> ? +[^\\])++>)";
string send = R"(.*<\\\w\w[^m-o][^_]++)";
string sdata = R"([^>]++>[^ ]++)";
regex endtag(send);
regex taganddata(sdata);
regex starttag(sstart);
Syntax of you regular expressions is incorrect because of '++' part.
.+ matches one or more occurrences. But what do you try to match with .++ ?

Do I need negative lookahead to extract TEST strings in my regular expression?

My goal is to remove tokens from the string below that do not start with "TEST" with the help of regular expressions.
TESTA=abc; VAL2=def; TESTB=ghi; TESTC=jkl; VAL2=bla1; VAL3=bla2
Based on reading online it seems I would need to create a regular expression that will match what I want and then use negative lookahead for it. However, I am unable to come up with one.
Input string:
TESTA=abc; VAL2=def; TESTB=ghi; TESTC=jkl; VAL2=bla1; VAL3=bla2
Matching string:
TESTA=abc; TESTB=ghi; TESTC=jkl;
Is it even possible to do what I want in a single regular expression?
We need this to place in our Apache conf file. Some of the cookies sent to Apache are so big that it is failing our application. The approach we are trying to take is to filter all the cookies not set by our application. We can enforce some sort of restriction that all our cookies start with specific prefix (as used in the example above) and we will filter the rest.
In Apache if I use the syntax below it will replace the cookie that has a key TESTC and its value from the string with empty string. I can enhance the regex to match is with key that starts with TEST_. So basically it can remove the following :> "; TEST_key:VALUE FOR Cookie" . However what I want is the exact opposite of it. Leave alone what matched and replace everything else with empty string.
RequestHeader edit Cookie "(^TESTC=[^;]; |; TESTC=[^;])" ""
Something like
[^=;]+(?<!TEST.)=[^;]+(?:$|;)
Regex Demo
What it does?
[^=;]+ Matches till the first = or ;
(?<!TEST.) Negative lookbehind. Checks if the string matched is preceded not by TEST.
=[^;]+ If the lookbehind is successful, Matches till the next ;
Use a Non-Greedy Quantifier
You don't need anything as complicated as zero-width assertions or negative lookahead or lookbehind. All you need is a non-greedy quantifier like *? in engines that support it. For example, at the Bash prompt and only using egrep:
$ echo 'TESTA=abc; VAL2=def; TESTB=ghi; TESTC=jkl; VAL2=bla1; VAL3=bla2' |
egrep -o 'TEST.*?;' | xargs
TESTA=abc; TESTB=ghi; TESTC=jkl;
You can do something similar in Ruby, Python, or Perl. For example, using Ruby:
str = 'TESTA=abc; VAL2=def; TESTB=ghi; TESTC=jkl; VAL2=bla1; VAL3=bla2'
str.scan(/TEST.*?;/).join " "
#=> "TESTA=abc; TESTB=ghi; TESTC=jkl;"