Jump to content
Sign in to follow this  
autoitxp

StringRegExpReplace properly !

Recommended Posts

autoitxp

HI

I have problem using StringRegExpReplace

$string get some html text here i wanted to filter some of special chars to cgi method like < is %3C i already set up in sequence help!

StringRegExpReplace($string ,  "<>="':?#[]!$&(),;%" , "%3C %3E %3D %22 %27 %3A %3F %23 %5B %5D %21 %24 %26 %28 %29 %2C %3B %25" )

Share this post


Link to post
Share on other sites
oMBRa

No, that replaces each match with the whole replace string, you have to replace each invalid char one at time, try this:

$String = " >=?%)"; string to replace

$sChars = "<>=""':?#[]!$&(),;%"
$sReplace = "%3C %3E %3D %22 %27 %3A %3F %23 %5B %5D %21 %24 %26 %28 %29 %2C %3B %25"
$aReplace = StringSplit($sReplace, ' ')
$aString = StringSplit($String, '')
Dim $iPos
Dim $NewString

For $i = 1 To $aString[0]
    $iPos = StringInStr($sChars, $aString[$i])
    If $iPos Then
        $aString[$i] = $aReplace[$iPos]
    EndIf
    $NewString &= $aString[$i]
Next

MsgBox(0, '', $NewString)

Share this post


Link to post
Share on other sites
autoitxp

Thank you Very much ! resolved

Share this post


Link to post
Share on other sites
Malkey

No, that replaces each match with the whole replace string, you have to replace each invalid char one at time, try this:

oMBRa

If you did not written, "you have to replace each invalid char one at time", I would not have attempted this.

;
         $String = " >=?%)"; string to replace
         
         $NewString =  Execute("'" & StringRegExpReplace($String,"([<>=""':?#\[\]!$(),;&%])",  "%' & hex(asc('\1'),2) & '" ) & "'")
         
         MsgBox(0, '', $NewString)
      ;

An interesting exercise that appears to work well.

Thanks.

Edit:Removed "{:content:}amp;", separated "$" and "&" in regular expression.

Edited by Malkey

Share this post


Link to post
Share on other sites
Authenticity

So I guess we should say thank you oMBRa and Malkey. :) It's truly impressive.

Share this post


Link to post
Share on other sites
autoitxp

yes really nice StringRegExpReplace really hard to understand

Edited by autoitxp

Share this post


Link to post
Share on other sites
Authenticity

It's not the StringRegExpReplace which is hard to grasp at first but with it's turned into in conjunction with the Execute() function. :) Try to use it like this, you'll understand it better:

ConsoleWrite("'" & StringRegExpReplace($String,"([<>=""':?#\[\]!{:content:}amp;(),;%])",  "%' & hex(asc('\1'),2) & '" ) & "'" & @LF)

Share this post


Link to post
Share on other sites
oMBRa

oMBRa

If you did not written, "you have to replace each invalid char one at time", I would not have attempted this.

;
 $String = " >=?%)"; string to replace
 
 $NewString =  Execute("'" & StringRegExpReplace($String,"([<>=""':?#\[\]!{:content:}amp;(),;%])",  "%' & hex(asc('\1'),2) & '" ) & "'")
 
 MsgBox(0, '', $NewString)
;

An interesting exercise that appears to work well.

Thanks.

That's a nice trick, I would have never guessed to use Execute to do operations in the replace parameter of StringRegExpReplace, this extends a lot its power

anyway what's "{:content:}" ? I think it means "$" but why you used it?

Edited by oMBRa

Share this post


Link to post
Share on other sites
Malkey

That's a nice trick, I would have never guessed to use Execute to do operations in the replace parameter of StringRegExpReplace, this extends a lot its power

anyway what's "{:content:}" ? I think it means "{:content:}quot; but why you used it?

I have edited my post with some difficulty. Apparently, having "$" and "&" next to each other displays as "{:content:}amp;" ({:content:}amp;) when posted to the forum. This is with the three Post Options not enabled.

I just noticed in the post edit window, your quote reads "anyway what's "{:content:}" ? I think it means "{:content:}quot; but why you used it?"

Share this post


Link to post
Share on other sites
Authenticity

There is still a problem. If you'll try to work with strings that contain a single or double quotes or both you'll get an incorrect string to pass to the Execute function:

Dim $sStr = 'abcde isn''t I "are" baboon'
Dim $sRegExp1 = "'" & StringRegExpReplace($sStr, "(?i)([a-z])([\w']*)", "' & _ProperCase('\1', '\2') & '") & "'"
Dim $sRegExp2 = '"' & StringRegExpReplace($sStr, '(?i)([a-z])([\w'']*)', '" & _ProperCase("\1", "\2") & "') & '"'
Dim $sRegExp3

ConsoleWrite(Execute($sRegExp1) & @LF)
ConsoleWrite(Execute($sRegExp2) & @LF)

$sStr = StringReplace($sStr, '"', Chr(0xF1))
$sRegExp3 = '"' & StringRegExpReplace($sStr, '(?i)([a-z])([\w'']*)', '" & _ProperCase("\1", "\2") & "') & '"'
$sStr = StringRegExpReplace(Execute($sRegExp3), '\xF1', '"')
ConsoleWrite($sStr & @LF)

Func _ProperCase($sLetter, $sWord = '')
    Return StringUpper($sLetter) & StringLower($sWord)
EndFunc

Share this post


Link to post
Share on other sites
Malkey

It would have been easier just to accept oMBRa's example. It works.

Having gone down this path, I need closure.

Here is a working Authenticity' script.

;
 Dim $sStr = 'abcde isn''t I "are" baboon'
 
 Dim $sRegExp1 = "'" & StringRegExpReplace($sStr, "(?i)([a-z])([\w']*)", "' & _ProperCase(""\1"", ""\2"") & '") & "'"
 
 ConsoleWrite($sRegExp1 & @LF & Execute($sRegExp1) & @LF)
 
 Func _ProperCase($sLetter, $sWord = '')
    Return StringUpper($sLetter) & StringLower($sWord)
 EndFunc  ;==>_ProperCase
;

Here is my modified script which allows the replacement of single and double quotes.

;
;$String = FileRead("E:\Program Files\AutoIt3\Extras\Editors\TextPad\Manual Install and Notes.htm");string to replace
 $String = ' >="?"% isn''t I "are" baboon'
 
 $NewString = Execute("'" & StringRegExpReplace($String, "([<>=':?#\[\]!$(),;&%])", "%' & hex(asc(""\1""),2) & '") & "'")
 
 $NewString = StringReplace($NewString, '"', "%22"); Replace "
 
 ConsoleWrite($NewString & @CRLF & @CRLF)
;

I believe the problem is - If """ (three double quotes) or ''' (three single quotes) or 'sn't' is generated by the StringRegExpReplace(), these create an invalid string, like a syntax error, and the Execute() will not work.

Share this post


Link to post
Share on other sites
GEOSoft

I know I'm dragging up an older thread but this is intriguing. I need an example of how I would use it to replace a string with FileRead("somefile.txt"). I know I should be able to figure it out but I'm brain-dead today.

What I'm actually doing is finding a comment block in an AutoIt Script and replace that with a comment block store in a separate text file.

Au3 file contains something like

#cs
    This is some
    commented block
    of special text
#ce

The text file only contains

#cs
    This is the
    replacement
    text.
#ce

If SRER worked the way we wanted it to, it would be a simple

$sStr = StringRegExpReplace($sStr, "(?is)(#cs.*special.*?#ce)", FileRead("textfile.txt"))

Jon should fix that. j/k


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!"

Share this post


Link to post
Share on other sites
trancexx

I know I'm dragging up an older thread but this is intriguing. I need an example of how I would use it to replace a string with FileRead("somefile.txt"). I know I should be able to figure it out but I'm brain-dead today.

What I'm actually doing is finding a comment block in an AutoIt Script and replace that with a comment block store in a separate text file.

Au3 file contains something like

#cs
    This is some
    commented block
    of special text
#ce

The text file only contains

#cs
    This is the
    replacement
    text.
#ce

If SRER worked the way we wanted it to, it would be a simple

$sStr = StringRegExpReplace($sStr, "(?is)(#cs.*special.*?#ce)", FileRead("textfile.txt"))

Jon should fix that. j/k

I fail to see the connection of your post to the rest of the thread. Is it me or you?

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
Malkey

GEOSoft

In this example, the replacement string, StringUpper($sReplacement), could be FileRead("filename"), or IniRead("C:\Temp\myfile.ini", "section2", "key", "NotFound"), or any representation of a string.

This example replaces only the "#cs .... #ce" comments which has the contents of the variable, $sSearchWithinComments, within the comment.

;
Local $sStr = FileRead("temp-38.au3"); A file with "#cs .. $sSearchWithinComments .... #ce" within.
Local $sReplacement = "#cs" & @LF & "    This is the" & @LF & "    replacement" & @LF & "    text." & @LF & "#ce"
Local $sSearchWithinComments = "special"

Local $sStr1 = StringRegExpReplace($sStr, "(?i)(\#cs.*\v+(.*[^#].*\v*).*" & $sSearchWithinComments & "(?s).*?\#ce)", StringUpper($sReplacement));

ConsoleWrite("@extended: " & @extended & "   Err: " & @error & @CRLF)
ConsoleWrite($sStr1 & @CRLF)
;

Share this post


Link to post
Share on other sites
GEOSoft

GEOSoft

In this example, the replacement string, StringUpper($sReplacement), could be FileRead("filename"), or IniRead("C:\Temp\myfile.ini", "section2", "key", "NotFound"), or any representation of a string.

This example replaces only the "#cs .... #ce" comments which has the contents of the variable, $sSearchWithinComments, within the comment.

;
Local $sStr = FileRead("temp-38.au3"); A file with "#cs .. $sSearchWithinComments .... #ce" within.
Local $sReplacement = "#cs" & @LF & "    This is the" & @LF & "    replacement" & @LF & "    text." & @LF & "#ce"
Local $sSearchWithinComments = "special"

Local $sStr1 = StringRegExpReplace($sStr, "(?i)(\#cs.*\v+(.*[^#].*\v*).*" & $sSearchWithinComments & "(?s).*?\#ce)", StringUpper($sReplacement));

ConsoleWrite("@extended: " & @extended & "   Err: " & @error & @CRLF)
ConsoleWrite($sStr1 & @CRLF)
;

Thanks Malkey. I was wondering really i there was some way of using Execute() in there to make it more pliable. For a single file replacement it's straight forward RexExpReplace code. I like code to be multi-pupose if possible and I'm sure it can be done, all I have to do is figure out how. In short I'm trying to approximate what I showed as an ideal situation in my post

And like I also said, I was brain dead yesterday, still recovering from a long all-nighter a couple of days before the post.

The one I'm actually working on can be done with

$sReplace = FileRead("File,txt")
$sString = StringRegExpReplace($sString, "(?is)(#cs.*special.*?#ce)", $sReplace)

Sooner or later I'll find time to work out a function that I can use to do this on multiple files, with multiple queries and multiple replacements.

Thanks for the time anyway.


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!"

Share this post


Link to post
Share on other sites
Authenticity

Why do you need this? The initial gain was to achieve something like PCRE callback procedure which is not available in AutoIt yet. If the replacement has nothing to do a function invocation you're requiring regular expression capabilities you don't need this Execute thing. In my opinion at least.

Share this post


Link to post
Share on other sites
GEOSoft

Why do you need this? The initial gain was to achieve something like PCRE callback procedure which is not available in AutoIt yet. If the replacement has nothing to do a function invocation you're requiring regular expression capabilities you don't need this Execute thing. In my opinion at least.

It's the limitation that I'm trying to work around. I'm not yet going to ask Jon to rewrite the RegExpReplace to include a callback proceedure because I know he had nightmares writing it the first time and I cringe thinking of his reply when asked to change it.

I was clutching at straws here and I'm seriously running out of hope but I may die trying to do it.

We can drop this now and chalk it up to a bad idea in general but thanks for the input anyway. As you know, I turn to you once in a while for RegExp and I'm sure if you say no then no it is.


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!"

Share this post


Link to post
Share on other sites
Malkey

Here is an effort to clear up my own confusion.

There is a pcre-callout feature, and pcre-back referencing. There is no pcre-callback.

This "Ticket #588 (assigned Feature Request)", opened 11 months ago, called StringRegExpReplaceCallback, is referring to a PHP function called "preg_replace_callback".

I believe there are no feature request for the pcre-callout feature.

I was totally amazed to Google find "Regular Expression Callouts" at AutoHotKey, with access to the pcre_callout_block structure, here.

Very, very impressive.

Edit: updated link.

Edited by Malkey

Share this post


Link to post
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
Sign in to follow this  

×