Jump to content

Please help me to understand the conditionals of StringRegExp()


Recommended Posts

I need help understanding the use of conditionals in StringRegExp() as I don't quite understand the info in the help file Please help 🙂

 

 

StringRegExp() help doc  https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm

 

 

 

 

These constructs are similar to If...EndIf and If...Else...EndIf blocks.

(?(condition)yes-pattern) Allows conditional execution of pattern.
(?(condition)yes-pattern|no-pattern) Chooses between distinct patterns depending on the result of (condition).
(n) Tests whether the capturing group with absolute number n matched.
(+n) Tests whether the capturing group with relative number +n matched.
(-n) Tests whether the capturing group with relative number -n matched.
(<name>) Tests whether the capturing group with name name matched.
(R) Tests whether any kind of recursion occured.
(Rn) Tests whether the most recent recursion was for capturing group with absolute number n.
(R&name) Tests whether the most recent recursion was for capturing group with name name.
(DEFINE) Used without no-pattern, permits definition of a subroutine useable from elsewhere. "(?x) (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )" defines a subroutine named "byte" which matches any component of an IPv4 address. Then an actual address can be matched by "\b (?&byte) (\.(?&byte)){3} \b".
(assertion) Here assertion is one of positive or negative, look-ahead or look-behind assertion.

SRE .JPG

Link to comment
Share on other sites

When you look in the help file that is in the AutoIt suite that you installed on your PC, look up the Tutorial - Regular Expression. Scrool to the bottom and you will see a button that says "Open StringRegExpGUI.au3"

This will open the au3 file in SciTE. Run it. The script is a tool to test strings. This will make it easy for you to try different things and learn it all fast.

Look at the attached picture and you will know exactly what to look for.

regex.png.ebc9b0c98df4fc573c493f39a2c0f525.png

When you run it, the app will look like this:

image.png.fa6ccdfee7716a1aa687dc4c8ace893d.png

Edited by Bert
Link to comment
Share on other sites

Sure you understand the similarity between If..Then..Else and the regex construct (?(condition)yes-pattern|no-pattern).

Now what is a condition in this kind of regex construct? They are listed in the following lines of the same table.

Be specific about what exactly is unclear to you.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

4 hours ago, jchd said:

Sure you understand the similarity between If..Then..Else and the regex construct (?(condition)yes-pattern|no-pattern).

Now what is a condition in this kind of regex construct? They are listed in the following lines of the same table.

Be specific about what exactly is unclear to you.

I know about the  If..Then..Else EndIf  conditional but  I have no clue on how to construct the StringRegExp conditionals I have tried but I must be doing something wrong check the following  example I can't make it work please help I want it to reply Yes or No

 

 
#include<Array.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>




$Reg = StringRegExp("My name is Manuel", "(Manuel)(?(1)Yes|No))"  ,1)

if IsArray($Reg) Then
    _ArrayDisplay($Reg)
Else
    ConsoleWrite($Reg & @CR)
    EndIf

 

Edited by Mannyfresh31
Typo
Link to comment
Share on other sites

38 minutes ago, Mannyfresh31 said:

I know about the  If..Then..Else EndIf  conditional

Before you tackle RegEx, you should know that this shorthand for conditionals is also available outside of RegEx.

For instance, this

If 1 = 1 Then 
   MsgBox($MB_OK, "Result: 1=1", "True!")
Else
   MsgBox($MB_OK, "Result: 1=1", "False!")
EndIf

can also be stated using the ternary operator, thusly:

(1 = 1) ? MsgBox($MB_OK, "Result: 1=1", "True!") : MsgBox($MB_OK, "Result: 1=1", "False!")

once you’re clear on that it should be easier to incorporate it into your RegEx.

Code hard, but don’t hard code...

Link to comment
Share on other sites

3 hours ago, JockoDundee said:

Before you tackle RegEx, you should know that this shorthand for conditionals is also available outside of RegEx.

For instance, this

If 1 = 1 Then 
   MsgBox($MB_OK, "Result: 1=1", "True!")
Else
   MsgBox($MB_OK, "Result: 1=1", "False!")
EndIf

can also be stated using the ternary operator, thusly:

(1 = 1) ? MsgBox($MB_OK, "Result: 1=1", "True!") : MsgBox($MB_OK, "Result: 1=1", "False!")

once you’re clear on that it should be easier to incorporate it into your RegEx.

I Know about if then..and ternary but the problem here is that 

 

 

#include<Array.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>




$Reg = StringRegExp("My name is Manuel", "(?<named>name)(?(<named>)(My)|(is))",3)

if IsArray($Reg) Then
    _ArrayDisplay($Reg)
Else
    ConsoleWrite($Reg & @CR)
    EndIf

the code above doesn't work while the code below does or atleast return something not what i want because I dont see the conditional to work just a match returned which is pointless to me if someone can clarify this thing for me I would appreciate a lot

 

#include<Array.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>




$Reg = StringRegExp("My name is Manuel", "(?<named>name)(?(<named>))",3)

if IsArray($Reg) Then
    _ArrayDisplay($Reg)
Else
    ConsoleWrite($Reg & @CR)
    EndIf

 

Link to comment
Share on other sites

1 hour ago, JockoDundee said:

Ok, but what are you trying to do?

What is the "correct" output that you expect?

 

I want or I expect something like Ternary and if then... look let me explain it to you

$Reg = StringRegExp("My name is Manuel", "(?<named>name)(?(<named>)(My)|(is))",3)

if  (?(<named>) is true meaning if the group name "named" is found since it has been declared "named" by (?<named>name) it should output (My) since (My) is in the conditional yes (?(<named>)(My)|(is)) and if group name "named" is not found return "is" as it is in the No conditiomal 

Edited by Mannyfresh31
Link to comment
Share on other sites

Your first syntax is invalid (extra closing parenthesis).

Using your original code, fixed:

StringRegExp("My name is Manuel", "(Manuel)(?(1)yes|no)", 1)

In your regex, the yes and no parts are patterns, not literal strings to be output!

Given your input and your regex pattern, the engine first looks for the string Manuel. Once found it captures it as group 1.

It then processes the conditional part of the regex:
1) the conditional is TRUE (group 1 matched), so it follows with the true-pattern which is yes.
2) the string yes is not found at the current position in the subject (it's at the end of subject) so the whole conditional fails (hence the whole regex).
3) @error is set to 1 as the helpfile says (Array is invalid. No matches) because the option is 1 (Return array of matches).

Checking the presence of a substring in a subject is simply done with StringInString().

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

A tutorial on regex conditionals: https://www.rexegg.com/regex-conditionals.html

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

As mentioned, SRE is all about capturing.  So the then and the else are part of the capturing action of the expression.  So if you want to get your (yes/no) answer you would need to use something like this :

MsgBox (0, "Searching Manuel", StringRegExp("My name is Manuel", "Manuel") ? "Yes" : "No")

But if you want to perform a conditional capture, here a few examples that can help you understand how it is working 

ConsoleWrite (StringRegExp("xyz", "(x)?y(?(1)z|w)") & @CRLF) ; success as there's a x followed by y with cond true of z
ConsoleWrite (StringRegExp("xya", "(x)?y(?(1)z|w)") & @CRLF) ; failure as there's a x followed by y but cond true of z fails on a
ConsoleWrite (StringRegExp("ayw", "(x)?y(?(1)z|w)") & @CRLF) ; success as there's no x followed but y with cond false of w
ConsoleWrite (StringRegExp("yw", "(x)?y(?(1)z|w)") & @CRLF) ; success as there's no x followed but y with cond false of w

Notice that the first group is (x)? and not (x?).  It is because you always want a successful capture (either a x or nothing).  If the group was to fail, then the whole expression would fail.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...