Jump to content

StringRegExp and StringRegExpReplace


 Share

Recommended Posts

Hello. I do know that it is already described in the help file, but I just don't get it how to use those two functions(StringRegExp and StringRegExpReplace). I wonder if you could explain it step by step to me(as if I were complete novice) giving some clear examples based on what do we want to achieve and what should we put into the parameters to achieve our goals.

Edited by zwierzak
Link to comment
Share on other sites

  • Moderators

zwierzak,

I wonder if you could explain it step by step to me

That would take weeks! ;)

I recommend this site - it is where I learnt what little I know about SREs (other than from GEOSoft's patient tuition) and I still use it a lot for reference. ;)

Good luck! :)

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

  • Moderators

Robjong,

Aha! I got there first with the SRE URL this time! ;)

Edit: Just seen your edit! :)

zwierzak,

SREs are the hardest thing I have ever tried to learn in computer terms - and they still make my brain bleed now. But they are very useful and being able to produce even simple ones will make your life as a coder very much easier. Fortunately you have a good few SRE gurus here who, if you have made some effort first, will help you develop much better patterns. ;)

M23

Edited by Melba23
Fun!

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

There is no way to explain it in a few lines, but basically it is a method of matching patterns, in a pattern there are meta/control characters which modify the behavior of previous or following characters, there are character sets and capturing groups, which technically are not capturing in some situations and... that's enoguh, but seriously it really is to much to explain in one go.

Regular Expressions (a.k.a. SRE, PCRE, Regex, RegExp) can be used for simple tasks like matching a digit but even that can be done in a number of ways.

These are all valid ways to match a single digit, nothing more then that.

Global $sText = 'Lorem ipsum 1' ; the text to match against
; a few patterns that all just match a single digit
Global $aPatterns[100] = ["[0-9]", _
        "[d]", _
        "[^D]", _
        "d", _
        "(?:d)", _
        "(?=d)", _
        "(?<=d)", _
        "[0123456789]", _
        "(0|1|2|3|4|5|6|7|8|9)", _
        "[x30-x39]", _
        "(x30|x31|x32|x33|x34|x35|x36|x37|x38|x39)", _
        "[[:digit:]]", _
        "[x{030}-x{039}]", _
        "(x{030}|x{031}|x{032}|x{033}|x{034}|x{035}|x{036}|x{037}|x{038}|x{039})"]

For $i = 0 To UBound($aPatterns) - 1 Step 1 ; loop over patterns
    If Not $aPatterns[$i] Then ExitLoop ; exit loop if there are no more patterns
    ConsoleWrite(StringRegExp($sText, $aPatterns[$i]) & ' @error=' & @error & ' @extended=' & @extended & " pattern=""" & $aPatterns[$i] & """" & @CRLF)
Next

See how something that seems simple could be pretty complicated? (if you make it so)

Now imagine trying to match something thats a bit more complicated, like any valid date format for example, it can get tricky to say the least.

I hope this gives you some insight as to the learning curve you will have to go through, but don't worry it all makes sense once you start getting the hang of it, and in the end it will be worth the time and frustration. :)

Edit: more blah blah and small script change

Edited by Robjong
Link to comment
Share on other sites

I can give you an example of StringRegExpReplace that I used in one of my own scripts:

Func _account($account)
    If $account > 0 Then
        If StringRegExp($account, 'D', 0) = 1 Then
            $account=StringRegExpReplace($account, 'D', "")
        EndIf
        Return $account
    Else
        $account = 0
        Return $account
    EndIf
EndFunc

This snippet is used in a MICR Check Reading program that I've developed. The main code looks at the account number of the MICR line data, and captures it. However, sometimes there are special characters in the middle of the account number that gets misrepresented. When this happens, it returns this character as a "B" letter.

$account=StringRegExpReplace($account, 'D', "")

The above performs StringRegExpReplace() on the account number, looking for any non-digit characters, and removing it completely. The 3rd argument is the replacement character, and if you leave it like how I have it, it will just remove it instead. In the 2nd argument, you specify the target to replace. My target was a special RegExp switch D which means all non-digit characters ignoring case.

See Here at the bottom of the page for a list of RegExp characters.

Edited by mechaflash213
Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

Another excellent site for teaching yourself Regular Expressions. Takes you from naught to Hell.

Don't let the cryptic appearance of REs stop you from learning. What you're gonna learn is worth its salt, even outside RE world.

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