Jump to content

How to return match found with StringRegExp?


USSSR
 Share

Go to solution Solved by jchd,

Recommended Posts

Hello all, I have been trying to figure out how to use StringRegExp flagging. This would be a small part of my very long code. Tried to search the forum, google and youtube but no luck. 😥

At first to get started I would need to use StringRegExp to find a string which would have 9 digits. For example "475910324". If this is found then I would need flagging to show/use it. How is it done?

#include <GuiConstants.au3>;Functions needed for most GUI Code
#include <Constants.au3>


Local $sData1 = "This is an example text which may include the correct thing 123456789. This is not 42345678.9 correct. This is not correct either as it contains 10 digits:1234567890"

If  StringRegExp($sData1,"\d9") then
            ConsoleWrite("Match found in Data1: " & $sData1&@CRLF) ;How to express only the found expression not the whole text in $sData1? I would need only the "123456789" instead of complete $sData1.

         Else
            ConsoleWrite("Match not found" &@CRLF)
EndIf
Exit

 

Link to comment
Share on other sites

  • Moderators

USSSR,

I would use the $STR_REGEXPARRAYFULLMATCH flag to return an array of all matches:

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

Local $sData1 = "This is an example text which may include the correct thing 123456789. This is not 42345678.9 correct. This is not correct either as it contains 10 digits:1234567890"

$aRet = StringRegExp($sData1,"\d{9}(?!\.)", $STR_REGEXPARRAYGLOBALMATCH)

_ArrayDisplay($aRet, "", Default, 8)

The pattern asks for all exact 9 digit strings, not followed by a decimal point to exclude the second case.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

28 minutes ago, Melba23 said:

USSSR,

I would use the $STR_REGEXPARRAYFULLMATCH flag to return an array of all matches:

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

Local $sData1 = "This is an example text which may include the correct thing 123456789. This is not 42345678.9 correct. This is not correct either as it contains 10 digits:1234567890"

$aRet = StringRegExp($sData1,"\d{9}(?!\.)", $STR_REGEXPARRAYGLOBALMATCH)

_ArrayDisplay($aRet, "", Default, 8)

The pattern asks for all exact 9 digit strings, not followed by a decimal point to exclude the second case.

M23

 

Thanks for super quick response. You are very helpful.

I have more to figure out but I have to think by myself a while if I find a solution... If I cant get it done I have to ask more help.

 

 

Link to comment
Share on other sites

StringRegExp() help is your closest friend. Then RegExp is a very useful test bench.

Edited by jchd

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

3 hours ago, USSSR said:

How is it done?

A possible way, using word boundary  \b

Local $sData1 = "This is an example text which may include the correct thing 123456789. This is not 42345678.9 correct. This is not correct either as it contains 10 digits:1234567890"

$res = StringRegExp($sData1,"\b\d{9}\b", 1)
Msgbox(0,"", $res[0])

 

Link to comment
Share on other sites

Thanks for your support. My skills are not as good as I thought :D I cant make an if expression to work..

 

What is wrong here?

 

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

Local $sData1 = "This is an example text which may include the correct thing in most cases: tips 123456789. This is not 42345678.9 correct. This is not correct either as it contains 10 digits: 1234567890, this is not accepted either ddw123456789. "

$aRet = StringRegExp($sData1, "\d{9}(?!\.)", $STR_REGEXPARRAYGLOBALMATCH)
$bRet = StringRegExp($sData1, "(?i)tips")

If $aRet = True and $bRet = True Then

ConsoleWrite("TIPS AND NUMBER FOUND: " & $aRet[0] &@CRLF)

ElseIf $aRet = True Then

ConsoleWrite("ONLY NUMBER FOUND: " & $aRet[0] &@CRLF)

Else

ConsoleWrite("NUMBER NOT FOUND"&@CRLF)

EndIf

Exit

 

Link to comment
Share on other sites

Regular expressions (RE) are in substance a series of arguments passed to a program (the RE engine), expressing what to search with explicit conditions.

Hence it requires the same rigor to define formally (with mathematical rigor) what you consider to be a match.

4 hours ago, USSSR said:

I would need to use StringRegExp to find a string which would have 9 digits.

This phrasing from your first post doesn't seem to match what you tell us now. It looks like you need to match a series of exactly 9 digits preceeded and followed by whitespace(s), but only you can tell. Is that a correct definition for your use case?

Edited by jchd

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

3 minutes ago, jchd said:

Regular expressions (RE) are in substance a series of arguments passed to a program (the RE engine), expressing what to search with explicit conditions.

Hence it requires the same rigor to define formally (with mathematical rigor) what you consider to be a match.

This phrasing from your first post doesn't seem to match what you tell us now. It looks like you need to match a series of exactly 9 digits preceded and followed by whitespace(s), but only you can tell. Is that a correct definition for your use case?

Hi, sorry for being unclear. Basically the first post is still correct and I need 9 digits to be preceded by a whitespace(s) but it can be followed by whitespace(s) or a "." This is not 100 % but will tackle out most of the cases.

For making the code more "bulletproof" I would like to find 9 digits as above and a an additional word "tips". If both of these are found then code would continue and do its magic. If only the 9 digit number is found I will make a popup for me to make a choise what to do.

The biggest challenge for me is how to declare(?) the 9 digits (if its found)...

Link to comment
Share on other sites

20 minutes ago, USSSR said:

What is wrong here?

Only when the third parameter is 0 (or skipped), it will return a boolean, otherwise it returns an array...So testing an array to be true is not the best idea !

Link to comment
Share on other sites

Would the string "blah fingertips match as well, when 987654321 is also there" match number and tips?

Is "tips" always before number ?

 

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

17 minutes ago, jchd said:

Would the string "blah fingertips match as well, when 987654321 is also there" match number and tips?

Is "tips" always before number ?

 

Good point! "tips" can be preceded by a whitespace(s) but it can be followed by whitespace(s) or a "."

Link to comment
Share on other sites

  • Solution

Would this be a correct behavior?

Local $aData = [ _
    "This is an example text which may include the correct thing in most cases: tips 123456789.", _
    "This is not 42345678.9 correct.", _
    "This is not correct either as it contains 10 digits: 1234567890,", _
    "this is not accepted either ddw123456789. ", _
    "tips 111111111", _
    "222222222 tips", _
    "333333333", _
    "tips.444444444", _
    " 555555555 tips.", _
    " tips 666666666.", _
    " 777777777 ", _
    " 888888888." _
]

For $s In $aData
    Local $a, $b
    $b = StringRegExp($s, "(?i)(?<= )tips(?= |\.)")
    $a = StringRegExp($s, "(?<= )(\d{9})(?= |\.)", $STR_REGEXPARRAYGLOBALMATCH)

    If @error Then
        ConsoleWrite("NUMBER NOT FOUND" & @CRLF)
    ElseIf $b Then
        ConsoleWrite("TIPS AND NUMBER FOUND: " & $a[0] & @CRLF)
    Else
        ConsoleWrite("ONLY NUMBER FOUND: " & $a[0] & @CRLF)
    EndIf
Next

 

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

Hi @jchd,

I would say yes 👍 , but I am excited what the others will say.

One little side note:
Two example strings, [0] and [9], in the $aData array are basically the same as far as I can see.

Best regards
Sven

________________
Stay innovative!

Edited by SOLVE-SMART

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

3 hours ago, jchd said:

Would this be a correct behavior?

Local $aData = [ _
    "This is an example text which may include the correct thing in most cases: tips 123456789.", _
    "This is not 42345678.9 correct.", _
    "This is not correct either as it contains 10 digits: 1234567890,", _
    "this is not accepted either ddw123456789. ", _
    "tips 111111111", _
    "222222222 tips", _
    "333333333", _
    "tips.444444444", _
    " 555555555 tips.", _
    " tips 666666666.", _
    " 777777777 ", _
    " 888888888." _
]

For $s In $aData
    Local $a, $b
    $b = StringRegExp($s, "(?i)(?<= )tips(?= |\.)")
    $a = StringRegExp($s, "(?<= )(\d{9})(?= |\.)", $STR_REGEXPARRAYGLOBALMATCH)

    If @error Then
        ConsoleWrite("NUMBER NOT FOUND" & @CRLF)
    ElseIf $b Then
        ConsoleWrite("TIPS AND NUMBER FOUND: " & $a[0] & @CRLF)
    Else
        ConsoleWrite("ONLY NUMBER FOUND: " & $a[0] & @CRLF)
    EndIf
Next

 

Thanks this solves the problem. I just have to adjust it a bit to fit in to my code. Thank you for the help.

Link to comment
Share on other sites

You'll note that the expression I posted is a large alternation: tips before of after number. In case you know tips will always be found before (resp. after) the number, you can simplify the expression by removing the unwanted after (resp. before) branch.

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

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...