Jump to content

stringregexp issue


Recommended Posts

 
I have a string to search for 4 different things, with four different results

                       $sText = StringReplace($sText2, "'", "")
                        $sText = StringReplace($sText, "/", "-")
                        $sText = StringReplace($sText, " ", "_")
                        $sText = StringReplace($sText, "&", "And")

Can all three be combined into one regexp? Thanks

Edited by Champak
Link to comment
Share on other sites

  • Developers

@ad777 mmm not even close to what is asked! A shame the OP didn't post a runnable replicator script, but that was easy in this case:

$sText = " this is a test '  /   $ "
$sText = StringReplace($sText, "'", "")
$sText = StringReplace($sText, "/", "-")
$sText = StringReplace($sText, " ", "_")
$sText = StringReplace($sText, "&", "And")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sText = ' & $sText & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

$sText = " this is a test '  /   $ "
$sText =  StringRegExpReplace($sText, '(?m)..','-_',1)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sText = ' & $sText & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

@Champak,

I normally make  array that contains the from & to replacement and loop through the array doing a stringreplace()

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks.

However, neither work.

$sText = "Jack's birthday is 02/02/02 & Jill's is the same"
$sText =  StringRegExpReplace($sText, '(?m)..','-_',1)
ConsoleWrite($sText)

This is what the result should be: "Jacks_birthday_is_02-02-02_And_Jills_is_the_same"

Putting stringreplace in an array is a good idea, I never thought about that. I'll give it a try, but the reason I wanted it with stringregexp is I thought 1/ it would be faster, and 2/ I'd prefer it inline with the rest of this particular passage of code I have.

Edited by Champak
Link to comment
Share on other sites

1 hour ago, Champak said:

2/ I'd prefer it inline with the rest

So just build a func (using Jos' method or not)

$txt = "Jack's birthday is 02/02/02 & Jill's is the same"
Msgbox(0,"", _Job($txt) )

;----------------------

Func _Job($sText)
    $sText = StringReplace($sText, "'", "")
    $sText = StringReplace($sText, "/", "-")
    $sText = StringReplace($sText, " ", "_")
    $sText = StringReplace($sText, "&", "And")
    Return $sText
EndFunc

Regex is magic but unfortunately it has limits
How frustrating :idiot:

 

 

Link to comment
Share on other sites

When you have a fix translate table and need to use it  many times, you could make it this way:

Local $mXlate[]
$mXlate["A"] = "Z"
$mXlate["B"] = "Y"
$mXlate["C"] = "X"
$mXlate["D"] = "W"
$mXlate["E"] = "V"
$mXlate["F"] = "U"
$mXlate["U"] = "F"
$mXlate["V"] = "E"
$mXlate["W"] = "D"
$mXlate["X"] = "C"
$mXlate["Y"] = "B"
$mXlate["Z"] = "A"

Local $sOut = Execute("'" & StringRegExpReplace("ABCDEF...UVWXYZ", "([A-FU-Z])", "' & $mXlate['$1'] & '") & "'")
ConsoleWrite($sOut & @LF)

 

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

For kicks, One overall working example 

$txt = "Jack's birthday is 02/02/02 & Jill's is the same"

MsgBox(0, '', _Replace($txt, "'/ &" & @TAB & "|-|_|And"))

MsgBox(0, '', _Replace($txt, "'/ &02" & @TAB & "| \ |   |And|12|34"))

MsgBox(0, '', _Replace($txt, "/&20" & @TAB & "1|may be||"))

Func _Replace($sTxt, $sSplit)
    Local $aSplit = StringSplit($sSplit, @TAB)
    Local $a1 = StringSplit($aSplit[1], "")
    Local $a2 = StringSplit($aSplit[2], "|")
    Local $sda = ObjCreate("Scripting.Dictionary")
    For $i = 1 To UBound($a1) - 1
        $sda.item($a1[$i]) = $a2[$i]
    Next
    Return Execute('"' & StringRegExpReplace($sTxt, "([" & $aSplit[1] & "])", '" & $sda.Item("$1") & "') & '"')
EndFunc

 

Edited by Deye
Link to comment
Share on other sites

@jchd

This code looks very interesting, but I don't really get it ( local dimming an array without giving the arrays size, filling later on with index addressing using one-letter-strings)

 

Local $mXlate[]
$mXlate["A"] = "Z"
$mXlate["B"] = "Y"
$mXlate["C"] = "X"
$mXlate["D"] = "W"
$mXlate["E"] = "V"
$mXlate["F"] = "U"
$mXlate["U"] = "F"
$mXlate["V"] = "E"
$mXlate["W"] = "D"
$mXlate["X"] = "C"
$mXlate["Y"] = "B"
$mXlate["Z"] = "A"

Local $sOut = Execute("'" & StringRegExpReplace("ABCDEF...UVWXYZ", "([A-FU-Z])", "' & $mXlate['$1'] & '") & "'")
ConsoleWrite($sOut & @LF)

 

Could you pls. drop some explaining lines for it?

TIA, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

This is a Map datatype, not an array. More or less an equivalent to a Scripting.Dictionary object (an indexed key-value store), but Maps are built-in beta version of AutoIt. See beta help on Variables > Maps & Arrays > Maps.

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

I see, very interesting!

Is this an equivalent to the powershell hash table variable type? Especially the aspect of access speed? In powershell even very large hash tables return the test // value results literally instantly.

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Exactly IIRC. Since in these type of containers the indexing is done and balanced on the fly as new entries are inserted, the access is O(log₂ N).

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

  • Moderators
8 hours ago, jchd said:

Maps are built-in beta version of AutoIt

Maps are now available in the latest release - 3.3.16.0.

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

Ah yes, I always hesitate between beta and release. My bad.

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