Jump to content

StringRegExReplace()


 Share

Recommended Posts

hi people, please help me!

how to i make this:

For $i = 1 To $tabadd[0]
        $sline = StringReplace($sline, $tabadd[$i] & "f(", $tabadd[$i] & "f (")
    Next
    For $i = 1 To $tabrem[0]
        $sline = StringReplace($sline, $tabrem[$i] & "f(", $tabrem[$i] & "f (")
    Next
    For $i = 1 To $tabadd[0]
        $sline = StringReplace($sline, $tabadd[$i] & "r(", $tabadd[$i] & "r (")
    Next
    For $i = 1 To $tabrem[0]
        $sline = StringReplace($sline, $tabrem[$i] & "r (", $tabrem[$i] & "r (")
    Next
    For $i = 1 To $tabadd[0]
        $sline = StringReplace($sline, $tabadd[$i] & "e(", $tabadd[$i] & "e (")
    Next
    For $i = 1 To $tabrem[0]
        $sline = StringReplace($sline, $tabrem[$i] & "e(", $tabrem[$i] & "e (")
    Next
    For $i = 1 To $tabadd[0]
        $sline = StringReplace($sline, $tabadd[$i] & "o(", $tabadd[$i] & "o (")
    Next
    For $i = 1 To $tabrem[0]
        $sline = StringReplace($sline, $tabrem[$i] & "o(", $tabrem[$i] & "o (")
    Next

using stringregexreplace() ?

I've tried everything and I can not work = (

thanks in advance!

Link to comment
Share on other sites

  • Moderators

golfinhu,

Does what you have posted work? If so, why do you want to use an SRE instead? ;)

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

darkshark,

Why do people insist on using SREs for things that do not need them? ;)

In your case 2 simple lines (<<<<<<<<<<<<<<<) are all you need: :)

#Include <String.au3>

$sString = 'Server.ScriptTimeout = "60"'

$sValue = _StringBetween($sString, '"', '"') ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

ConsoleWrite($sValue[0] & @CRLF)

$sNewString = StringReplace($sString, $sValue[0], "New_Value") ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

ConsoleWrite($sNewString & @CRLF)

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

Mat,

The point here is that the SRE in _StringBetween is invisible to the user (and me until you pointed it out!). ;)

Too many people want to use a "sexy" SRE when a simple AutoIt commands will suffice - as in this trivial case. For a long list of items with a reasonably repeatable pattern I quite agree that an SRE is the way to go - but as GEOSoft once said to me: "One of the major problems of using SREs is deciding when NOT to use one" (or words to that effect). :)

What are we going to do with you

You are stuck with me - tough! ;)

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

For a single between thing (like the case above), I use:

Func _StringBetween2($sString, $sStart, $sEnd, $iCase = 0)
    $iStart = StringInStr($sString, $sStart, $iCase) + 1
    $iEnd  = StringInStr($sString, $sEnd, $iCase, -1)

    Return StringMid($sString, $iStart, $iEnd - $iStart)
EndFunc

Although it is usually messier than that and all on one line ;)

Link to comment
Share on other sites

M23, That's close enough to what I said to count and it still holds.

The general rule that I use for even simple lines is, IF it can be done easily using 1 or 2 simple String*() calls then use that method. If it's more complex, use an SRE. Testing has also shown me that in simple cases the String*() methods are just a tad faster than SRE's in many cases, so there are a few variables to consider when you make that decision. In the OP's case it's such a simple SRE that would do it all, he could easily use one. He's unnecessarily using embedded loops though.

For $i = 1 To UBound($tabadd) -1
    $tabadd[$i] = StringRegExpReplace($tabAdd[$i], "(?i)([freo])\s*\(", "$1 \(")
Next

Where

(?i) means it's not case sensitive

[freo] means match any one of those characters

\s* means match zero or more spaces

\( means it must be be followed with (

Then

Replace the matching strings with the letter found in the first part followed by a space and a (

In his case I would probably opt for the SRER method because of the variables. Of course that all depends on whether or not I correctly read what he wanted to do in that mess of loops. Depending on what he is actually doing in the rest (unposted portion) of the script there may not even be a need for the outer loop. For example, if he's reading a page of text and then trying to put each line into an array element before he loops through to correct the lines then he's wasting time, he could run the SRER against the whole file at once instead.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

M23, That's close enough to what I said to count and it still holds.

The general rule that I use for even simple lines is, IF it can be done easily using 1 or 2 simple String*() calls then use that method. If it's more complex, use an SRE. Testing has also shown me that in simple cases the String*() methods are just a tad faster than SRE's in many cases, so there are a few variables to consider when you make that decision. In the OP's case it's such a simple SRE that would do it all, he could easily use one. He's unnecessarily using nested loops though.

For $i = 1 To UBound($tabadd) -1
    $tabadd[$i] = StringRegExpReplace($tabAdd[$i], "(?i)([freo])\s*\(", "$1 \(")
Next

Where

(?i) means it's not case sensitive

[freo] means match any one of those characters

\s* means match zero or more spaces

\( means it must be be followed with (

Then

Replace the matching strings with the letter found in the first part followed by a space and a (

In his case I would probably opt for the SRER method because of the variables. Of course that all depends on whether or not I correctly read what he wanted to do in that mess of loops. Depending on what he is actually doing in the rest (unposted portion) of the script there may not even be a need for the outer loop. For example, if he's reading a page of text and then trying to put each line into an array element before he loops through to correct the lines then he's wasting time, he could run the SRER against the whole file at once instead.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

M23, That's close enough to what I said to count and it still holds.

The general rule that I use for even simple lines is, IF it can be done easily using 1 or 2 simple String*() calls then use that method. If it's more complex, use an SRE. Testing has also shown me that in simple cases the String*() methods are just a tad faster than SRE's in many cases, so there are a few variables to consider when you make that decision. In the OP's case it's such a simple SRE that would do it all, he could easily use one. He's unnecessarily using embedded loops though.

For $i = 1 To UBound($tabadd) -1
    $tabadd[$i] = StringRegExpReplace($tabAdd[$i], "(?i)([freo])\s*\(", "$1 \(")
Next

Where

Then

Replace the matching strings with the letter found in the first part followed by a space and a (

In his case I would probably opt for the SRER method because of the variables. Of course that all depends on whether or not I correctly read what he wanted to do in that mess of loops. Depending on what he is actually doing in the rest (unposted portion) of the script there may not even be a need for the outer loop. For example, if he's reading a page of text and then trying to put each line into an array element before he loops through to correct the lines then he's wasting time, he could run the SRER against the whole file at once instead.

it worked!

the more I try, I can not understand regular expressions = (, has a site with good content to recommend me? then I can study!

get it!

I'm doing a TIDY for LUA (it's also a programming language)

then, the tests i did here, i think in my case SRER fared better, and leave the code cleaner!

but thanks for the script!

Edited by golfinhu
Link to comment
Share on other sites

You can use the PCRE toolkit in my signature to test and there are a couple of links to Regular Expression sites in the Help menu. There is also a good link in the Help file

http://www.autoitscript.com/autoit3/pcrepattern.html

That is generally a copy of the page fron the PCRE wesite but I think it was altered a wee bit to make it more compatable with PCRE as used in AutoIt. I could be wrong there since I haven't done a text comparison of the 2 pages.

The problem with linking to sites comes in when people are not familiar with the differences between different Regular Expression engines. They all have some things in common and they all have their own little differences.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 2 weeks later...

Not in any way that I can think of offhand.

You can do it with StringReplace by putting the string to replace and the replacement string into a 2 dimension array. Just loop through the array.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

You can also use multidimensional arrays with StringRegExpReplace like so...

#include <Array.au3>
Local $Text = 'ABC', $Result, $Header
Local $Array[3][2] = [['A', '"a replaced"'], ['B', '"b replaced"'], ['C', '"c replaced"']]
_Arraydisplay($Array, 'Starting Text')
For $i = 0 To UBound($Array) - 1
    $Header = 'Pass ' & $i + 1 & ' = '
    $Result &= $Header & StringRegExpReplace($Text, $Array[$i][0], $Array[$i][1]) & @LF
Next
MsgBox(32, '', $Result)
It does not fulfill your needs exactly, but it may help down the road

Edited by Varian
Link to comment
Share on other sites

You should show us exactly what you want returned from that string. I'm not sure if I understand what you want here. Maybe this?

$sStr = "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6"
$sStr = StringRegExpReplace($sStr, ".+?([1-5]+).+", "$1")
MsgBox(0, "result, $sStr)

You can test SREs with the PCRE Toolkit in my signature.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Can the "X" in this code be a variable that changes depending on the letter that it is replacing?

Other than something like this. Nope. (until proven other wise of course.)

$sText = 'de Aap had een Noot van Mies in zijn hand'
$spattern = 'Aa(p)|No(o)t|Mi(e)s'
$sreplace = '$1$2$3'
$result = StringRegExpReplace($sText, $spattern, $sreplace)
;; $result = "de p had een o van e in zijn hand"

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

why not stick to using that what seems more fitting to the (apparent) job. (Creating a single RE for this seems rather painful to me.)

Func _ParseNumbers($s_Str)
    Local $s_Ret = ""
    For $i = 1 To 5
        If StringInStr($s_Str, String($i)) Then $s_Ret &= String($i)
    Next
    Return $s_Ret
EndFunc ;==>_ParseNumbers
Func _ParseNumbers($s_Str)
    Local $s_Ret = "", $aChar[5] = ['1', '2', '3', '4', '5']
    For $e In $aChar
        If StringInStr($s_Str, $e) Then $s_Ret &= $e
    Next
    Return $s_Ret
EndFunc ;==>_ParseNumbers

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

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