AutoIt Forums: StringReplace but only exact matches - AutoIt Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

StringReplace but only exact matches

#1 User is offline   BennyBB 

  • Newbie
  • Group: Members
  • Posts: 4
  • Joined: 25-October 09

Posted 06 November 2009 - 07:33 PM

Hi there,
I'm even new to AutoIT and just occupied myself with a little script, that should replace some variables in a script.
It works like this:
You put your script into the clipboard. Then you tell the GUI what to replace e.g. $a should be replaced with $bb
The problem is, that i didnt find out, how to make string replace just replacing EXACT MATCHES.
E.g.:


$a = 55
$ab = 10
replace $a with $b
=> he returns
$b = 55
$bb = 10

But that's wrong :( I want to replace just $a and not $ab
=>
$b = 55
$ab = 10

Pleace help me :)


Here's the Script:
[ autoIt ]    ( ExpandCollapse - Popup )
#cs Code-Variable replacer by BennyBB #ce #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 625, 445, 192, 124) $Input1 = GUICtrlCreateInput("", 60, 40, 185, 21) $Input2 = GUICtrlCreateInput("", 370, 40, 185, 21) $Input6 = GUICtrlCreateInput("", 60, 90, 185, 21) $Input3 = GUICtrlCreateInput("", 370, 90, 185, 21) $Input7 = GUICtrlCreateInput("", 60, 140, 185, 21) $Input4 = GUICtrlCreateInput("", 370, 140, 185, 21) $Input8 = GUICtrlCreateInput("", 60, 190, 185, 21) $Input5 = GUICtrlCreateInput("", 370, 190, 185, 21) $input9 = GUICtrlCreateInput("", 60, 240, 185, 21) $input10 = GUICtrlCreateInput("", 370, 240, 185, 21) $input11 = GUICtrlCreateInput("", 60, 290, 185, 21) $input12 = GUICtrlCreateInput("", 370, 290, 185, 21) $Label1 = GUICtrlCreateLabel("$", 15, 40, 36, 17) $Label2 = GUICtrlCreateLabel("$", 15, 90, 36, 17) $Label3 = GUICtrlCreateLabel("$", 15, 140, 36, 17) $Label4 = GUICtrlCreateLabel("$", 15, 190, 36, 17) $Label5 = GUICtrlCreateLabel("wird zu     $", 300, 40, 76, 17) $Label6 = GUICtrlCreateLabel("wird zu     $", 300, 90, 60, 17) $Label7 = GUICtrlCreateLabel("wird zu     $", 300, 140, 60, 17) $Label8 = GUICtrlCreateLabel("wird zu     $", 300, 190, 60, 17) $Label9 = GUICtrlCreateLabel("$", 15, 240, 36, 17) $label10 = GUICtrlCreateLabel("wird zu     $", 300, 240, 60, 17) $label11 = GUICtrlCreateLabel("$", 15, 290, 36, 17) $label12 = GUICtrlCreateLabel("wird zu     $", 300, 290, 60, 17) $Button1 = GUICtrlCreateButton("Start", 176, 304, 273, 73, $WS_GROUP) $Button2 = GUICtrlCreateButton("Add", 24, 248, 121, 33, $WS_GROUP) GUICtrlSetState($input9,$GUI_HIDE) GUICtrlSetState($input10,$GUI_HIDE) GUICtrlSetState($input11,$GUI_HIDE) GUICtrlSetState($input12,$GUI_HIDE) GUICtrlSetState($label9,$GUI_HIDE) GUICtrlSetState($label10,$GUI_HIDE) GUICtrlSetState($label11,$GUI_HIDE) GUICtrlSetState($label12,$GUI_HIDE) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### $add = 0 While 1     $nMsg = GUIGetMsg()     Switch $nMsg         Case $GUI_EVENT_CLOSE             Exit         Case $Button1             $input1 = "$" & GUICtrlRead($input1)             $input2 = "$" & GUICtrlRead($input2)             $input3 = "$" & GUICtrlRead($input3)             $input4 = "$" & GUICtrlRead($input4)             $input5 = "$" & GUICtrlRead($input5)             $input6 = "$" & GUICtrlRead($input6)             $input7 = "$" & GUICtrlRead($input7)             $input8 = "$" & GUICtrlRead($input8)             If $add = 1 Then             $input9 = "$" & GUICtrlRead($input9)             $input10 = "$" & GUICtrlRead($input10)             $input11 = "$" & GUICtrlRead($input11)             $input12 = "$" & GUICtrlRead($input12)             EndIf             GUIDelete($form1)             replace ()         Case $button2             Guictrlsetstate($label9,$GUI_SHOW)             GUICtrlSetState($input10,$GUI_SHOW)             GUICtrlSetState($label11,$GUI_SHOW)             GUICtrlSetState($label12,$GUI_SHOW)             GUICtrlSetState($input9,$GUI_SHOW)             GUICtrlSetState($input12,$GUI_show)             GUICtrlSetState($input11,$GUI_SHOW)             GUICtrlSetState($label10,$GUI_SHOW)             GUICtrlSetPos($button1,176,354)             GUICtrlSetState($button2,$GUI_HIDE)             Dim $add = 1     EndSwitch WEnd Func replace () $code = CliPGet() MsgBox (0,"",$code) If $code = 1 Then     ToolTip ("No text in clipboard") ElseIf IsString($code) = 1 Then     $code = StringReplace ($code,$input1,$input2,0,1)     $code = StringReplace ($code,$input3,$input4,0,1)     $code = StringReplace ($code,$input5,$input6,0,1)     $code = StringReplace ($code,$input7,$input8,0,1)     $code = StringReplace ($code,$input9,$input10,0,1)     $code = StringReplace ($code,$input11,$input12,0,1)     MsgBox (0,"",$code)     ClipPut($code) EndIf Exit EndFunc  

0

#2 User is offline   memoryoverflow 

  • Member
  • Pip
  • Group: Full Members
  • Posts: 71
  • Joined: 26-September 09
  • Gender:Not Telling
  • Location:The Matrix (as you've guessed)

Posted 06 November 2009 - 08:11 PM

View PostBennyBB, on 06 November 2009 - 07:33 PM, said:

$a = 55
$ab = 10
replace $a with $b
=> he returns
$b = 55
$bb = 10

But that's wrong :( I want to replace just $a and not $ab
=>
$b = 55
$ab = 10
You don't really tell what the String is, in which you want the replacement to occur.
If it's the whole text, like:
Local $sText = '$a = 55' & @CRLF & '$ab = 10'
you could use
Local $sChanged = StringReplace($sText, '$a ', '$b ')

This post has been edited by memoryoverflow: 06 November 2009 - 08:12 PM

0

#3 User is online   Authenticity 

  • Mass Spammer!
  • Icon
  • Group: AutoIt MVPs(MVP)
  • Posts: 2,378
  • Joined: 22-January 09
  • Gender:Male

Posted 06 November 2009 - 09:05 PM

Few notes:

- When you want to treat a global variable, doing Dim $add = 1 doesn't set the global one if in context of another scope (like within another function) but create a local one, set it's value to 1 and the global $add is still 0 so take this into consideration. :)

- The inputs are not ordered correctly so it's something like:
[ code='text' ]    ( Popup )
input1          input2 input6          input3  

..making StringReplace to replace wrong content.

Have a look:
[ autoIt ]    ( ExpandCollapse - Popup )
#cs Code-Variable replacer by BennyBB #ce #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 625, 445, 192, 124) $Input1 = GUICtrlCreateInput("", 60, 40, 185, 21) $Input2 = GUICtrlCreateInput("", 370, 40, 185, 21) $Input6 = GUICtrlCreateInput("", 60, 90, 185, 21) $Input3 = GUICtrlCreateInput("", 370, 90, 185, 21) $Input7 = GUICtrlCreateInput("", 60, 140, 185, 21) $Input4 = GUICtrlCreateInput("", 370, 140, 185, 21) $Input8 = GUICtrlCreateInput("", 60, 190, 185, 21) $Input5 = GUICtrlCreateInput("", 370, 190, 185, 21) $input9 = GUICtrlCreateInput("", 60, 240, 185, 21) $input10 = GUICtrlCreateInput("", 370, 240, 185, 21) $input11 = GUICtrlCreateInput("", 60, 290, 185, 21) $input12 = GUICtrlCreateInput("", 370, 290, 185, 21) $Label1 = GUICtrlCreateLabel("$", 15, 40, 36, 17) $Label2 = GUICtrlCreateLabel("$", 15, 90, 36, 17) $Label3 = GUICtrlCreateLabel("$", 15, 140, 36, 17) $Label4 = GUICtrlCreateLabel("$", 15, 190, 36, 17) $Label5 = GUICtrlCreateLabel("wird zu     $", 300, 40, 76, 17) $Label6 = GUICtrlCreateLabel("wird zu     $", 300, 90, 60, 17) $Label7 = GUICtrlCreateLabel("wird zu     $", 300, 140, 60, 17) $Label8 = GUICtrlCreateLabel("wird zu     $", 300, 190, 60, 17) $Label9 = GUICtrlCreateLabel("$", 15, 240, 36, 17) $label10 = GUICtrlCreateLabel("wird zu     $", 300, 240, 60, 17) $label11 = GUICtrlCreateLabel("$", 15, 290, 36, 17) $label12 = GUICtrlCreateLabel("wird zu     $", 300, 290, 60, 17) $Button1 = GUICtrlCreateButton("Start", 176, 304, 273, 73, $WS_GROUP) $Button2 = GUICtrlCreateButton("Add", 24, 248, 121, 33, $WS_GROUP) GUICtrlSetState($input9,$GUI_HIDE) GUICtrlSetState($input10,$GUI_HIDE) GUICtrlSetState($input11,$GUI_HIDE) GUICtrlSetState($input12,$GUI_HIDE) GUICtrlSetState($label9,$GUI_HIDE) GUICtrlSetState($label10,$GUI_HIDE) GUICtrlSetState($label11,$GUI_HIDE) GUICtrlSetState($label12,$GUI_HIDE) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $add = 0 While 1     $nMsg = GUIGetMsg()     Switch $nMsg         Case $GUI_EVENT_CLOSE             Exit         Case $Button1             $input1 = "$" & GUICtrlRead($input1)             $input2 = "$" & GUICtrlRead($input2)             $input3 = "$" & GUICtrlRead($input3)             $input4 = "$" & GUICtrlRead($input4)             $input5 = "$" & GUICtrlRead($input5)             $input6 = "$" & GUICtrlRead($input6)             $input7 = "$" & GUICtrlRead($input7)             $input8 = "$" & GUICtrlRead($input8)             If $add = 1 Then                 $input9 = "$" & GUICtrlRead($input9)                 $input10 = "$" & GUICtrlRead($input10)                 $input11 = "$" & GUICtrlRead($input11)                 $input12 = "$" & GUICtrlRead($input12)             EndIf             GUIDelete($form1)             replace()             Exit         Case $button2             Guictrlsetstate($label9,$GUI_SHOW)             GUICtrlSetState($input10,$GUI_SHOW)             GUICtrlSetState($label11,$GUI_SHOW)             GUICtrlSetState($label12,$GUI_SHOW)             GUICtrlSetState($input9,$GUI_SHOW)             GUICtrlSetState($input12,$GUI_show)             GUICtrlSetState($input11,$GUI_SHOW)             GUICtrlSetState($label10,$GUI_SHOW)             GUICtrlSetPos($button1,176,354)             GUICtrlSetState($button2,$GUI_HIDE)             $add = 1     EndSwitch WEnd Func replace ()     $code = CliPGet()     If @error Then         ToolTip ("No text in clipboard")     ElseIf IsString($code) Then         MsgBox (0,"",$code)                     If StringLen($input1) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(^|\b)\Q" & $input1 & "\E", "\" & $input2)         If StringLen($input3) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(^|\b)\Q" & $input3 & "\E", "\" & $input4)         If StringLen($input5) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(^|\b)\Q" & $input6 & "\E", "\" & $input6)         If StringLen($input7) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(^|\b)\Q" & $input7 & "\E", "\" & $input8)         If $add = 1 Then             If StringLen($input9) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(^|\b)\Q" & $input9 & "\E", "\" & $input10)             If StringLen($input11) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(^|\b)\Q" & $input11 & "\E", "\" & $input12)         EndIf         MsgBox(0, "", $code)         ClipPut($code)     EndIf EndFunc  


It's using StringRegExpReplace to have more control on the search and replace pattern.
0

#4 User is offline   BennyBB 

  • Newbie
  • Group: Members
  • Posts: 4
  • Joined: 25-October 09

Posted 07 November 2009 - 09:58 AM

First of all Thanks for the fast answers and Tips

I even read about this RegExp stuff but I thought that would be too complicated for me with all these pattern stuff etc.
so, thanks for this part :)
I Fixed some of the bugs (e.g. the wrong input order)
but it's still not returning, what I wanted to be returned

Here is again a little example :
[ autoIt ]    ( Popup )
$a = 55 $ab = 10 $aab = 'aac' $uo = 745  

http://img97.imageshack.us/img97/8567/guiset1.th.png

but he returns

[ autoIt ]    ( Popup )
$q = 55 $qb = 10 $qab = 'aac' $wo = 745  


but actually I wanted to get this:
[ autoIt ]    ( Popup )
$q = 55 $ab = 10 $t = 'aac' $uo = 745  


Is this even possible??????
Thanks in advance


btw i forgot this :
this is the current code:
[ autoIt ]    ( ExpandCollapse - Popup )
#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 625, 445, 192, 124) $Input1 = GUICtrlCreateInput("", 60, 40, 185, 21) $Input2 = GUICtrlCreateInput("", 370, 40, 185, 21) $Input6 = GUICtrlCreateInput("", 60, 90, 185, 21) $Input3 = GUICtrlCreateInput("", 370, 90, 185, 21) $Input7 = GUICtrlCreateInput("", 60, 140, 185, 21) $Input4 = GUICtrlCreateInput("", 370, 140, 185, 21) $Input8 = GUICtrlCreateInput("", 60, 190, 185, 21) $Input5 = GUICtrlCreateInput("", 370, 190, 185, 21) $input9 = GUICtrlCreateInput("", 60, 240, 185, 21) $input10 = GUICtrlCreateInput("", 370, 240, 185, 21) $input11 = GUICtrlCreateInput("", 60, 290, 185, 21) $input12 = GUICtrlCreateInput("", 370, 290, 185, 21) $Label1 = GUICtrlCreateLabel("$", 15, 40, 36, 17) $Label2 = GUICtrlCreateLabel("$", 15, 90, 36, 17) $Label3 = GUICtrlCreateLabel("$", 15, 140, 36, 17) $Label4 = GUICtrlCreateLabel("$", 15, 190, 36, 17) $Label5 = GUICtrlCreateLabel("wird zu     $", 300, 40, 76, 17) $Label6 = GUICtrlCreateLabel("wird zu     $", 300, 90, 60, 17) $Label7 = GUICtrlCreateLabel("wird zu     $", 300, 140, 60, 17) $Label8 = GUICtrlCreateLabel("wird zu     $", 300, 190, 60, 17) $Label9 = GUICtrlCreateLabel("$", 15, 240, 36, 17) $label10 = GUICtrlCreateLabel("wird zu     $", 300, 240, 60, 17) $label11 = GUICtrlCreateLabel("$", 15, 290, 36, 17) $label12 = GUICtrlCreateLabel("wird zu     $", 300, 290, 60, 17) $Button1 = GUICtrlCreateButton("Start", 176, 304, 273, 73, $WS_GROUP) $Button2 = GUICtrlCreateButton("Add", 24, 248, 121, 33, $WS_GROUP) GUICtrlSetState($input9,$GUI_HIDE) GUICtrlSetState($input10,$GUI_HIDE) GUICtrlSetState($input11,$GUI_HIDE) GUICtrlSetState($input12,$GUI_HIDE) GUICtrlSetState($label9,$GUI_HIDE) GUICtrlSetState($label10,$GUI_HIDE) GUICtrlSetState($label11,$GUI_HIDE) GUICtrlSetState($label12,$GUI_HIDE) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Dim $add = 0 While 1     $nMsg = GUIGetMsg()     Switch $nMsg         Case $GUI_EVENT_CLOSE             Exit         Case $Button1             $input1 = "$" & GUICtrlRead($input1)             $input2 = "$" & GUICtrlRead($input2)             $input3 = "$" & GUICtrlRead($input3)             $input4 = "$" & GUICtrlRead($input4)             $input5 = "$" & GUICtrlRead($input5)             $input6 = "$" & GUICtrlRead($input6)             $input7 = "$" & GUICtrlRead($input7)             $input8 = "$" & GUICtrlRead($input8)             If $add = 1 Then                 $input9 = "$" & GUICtrlRead($input9)                 $input10 = "$" & GUICtrlRead($input10)                 $input11 = "$" & GUICtrlRead($input11)                 $input12 = "$" & GUICtrlRead($input12)             EndIf             GUIDelete($form1)             replace()             Exit         Case $button2             Guictrlsetstate($label9,$GUI_SHOW)             GUICtrlSetState($input10,$GUI_SHOW)             GUICtrlSetState($label11,$GUI_SHOW)             GUICtrlSetState($label12,$GUI_SHOW)             GUICtrlSetState($input9,$GUI_SHOW)             GUICtrlSetState($input12,$GUI_show)             GUICtrlSetState($input11,$GUI_SHOW)             GUICtrlSetState($label10,$GUI_SHOW)             GUICtrlSetPos($button1,176,354)             GUICtrlSetState($button2,$GUI_HIDE)             local $add = 1     EndSwitch WEnd Func replace ()     $code = CliPGet()     If @error Then         ToolTip ("No text in clipboard")     ElseIf IsString($code) Then         MsgBox (0,"",$code)                     If StringLen($input1) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(^|\b)\Q" & $input1 & "\E", "\" & $input2)         If StringLen($input6) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(^|\b)\Q" & $input6 & "\E", "\" & $input3)         If StringLen($input7) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(^|\b)\Q" & $input7 & "\E", "\" & $input4)         If StringLen($input8) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(^|\b)\Q" & $input8 & "\E", "\" & $input5)         If StringLen($input9) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(^|\b)\Q" & $input9 & "\E", "\" & $input10)         If StringLen($input11) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(^|\b)\Q" & $input11 & "\E", "\" & $input12)         MsgBox(0, "", $code)         ClipPut($code)     EndIf EndFunc  

This post has been edited by BennyBB: 07 November 2009 - 10:09 AM

0

#5 User is offline   Melba23 

  • Yes, me!
  • PipPipPipPipPipPipPip
  • Group: Full Members
  • Posts: 2,055
  • Joined: 01-August 08
  • Gender:Not Telling
  • Location:Where never lark or even eagle flew

Posted 07 November 2009 - 11:15 AM

BennyBB,

The SRE patterns were replacing all instances, not complete ones. Try this version - it gives me what you want in your last post:
[ autoIt ]    ( ExpandCollapse - Popup )
#cs Code-Variable replacer by BennyBB #ce #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $sText = "$a = 55" & @CRLF & "$ab = 10" & @CRLF & "$aab = 'aac'" & @CRLF & "$uo = 745" ClipPut($sText) #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 625, 445, 192, 124) $Input1 = GUICtrlCreateInput("", 60, 40, 185, 21) $Input2 = GUICtrlCreateInput("", 370, 40, 185, 21) $Input3 = GUICtrlCreateInput("", 60, 90, 185, 21) $Input4 = GUICtrlCreateInput("", 370, 90, 185, 21) $Input5 = GUICtrlCreateInput("", 60, 140, 185, 21) $Input6 = GUICtrlCreateInput("", 370, 140, 185, 21) $Input7 = GUICtrlCreateInput("", 60, 190, 185, 21) $Input8 = GUICtrlCreateInput("", 370, 190, 185, 21) $input9 = GUICtrlCreateInput("", 60, 240, 185, 21) $input10 = GUICtrlCreateInput("", 370, 240, 185, 21) $input11 = GUICtrlCreateInput("", 60, 290, 185, 21) $input12 = GUICtrlCreateInput("", 370, 290, 185, 21) $Label1 = GUICtrlCreateLabel("$", 15, 40, 36, 17) $Label2 = GUICtrlCreateLabel("$", 15, 90, 36, 17) $Label3 = GUICtrlCreateLabel("$", 15, 140, 36, 17) $Label4 = GUICtrlCreateLabel("$", 15, 190, 36, 17) $Label5 = GUICtrlCreateLabel("wird zu     $", 300, 40, 76, 17) $Label6 = GUICtrlCreateLabel("wird zu     $", 300, 90, 60, 17) $Label7 = GUICtrlCreateLabel("wird zu     $", 300, 140, 60, 17) $Label8 = GUICtrlCreateLabel("wird zu     $", 300, 190, 60, 17) $Label9 = GUICtrlCreateLabel("$", 15, 240, 36, 17) $label10 = GUICtrlCreateLabel("wird zu     $", 300, 240, 60, 17) $label11 = GUICtrlCreateLabel("$", 15, 290, 36, 17) $label12 = GUICtrlCreateLabel("wird zu     $", 300, 290, 60, 17) $Button1 = GUICtrlCreateButton("Start", 176, 304, 273, 73, $WS_GROUP) $Button2 = GUICtrlCreateButton("Add", 24, 248, 121, 33, $WS_GROUP) GUICtrlSetState($input9,$GUI_HIDE) GUICtrlSetState($input10,$GUI_HIDE) GUICtrlSetState($input11,$GUI_HIDE) GUICtrlSetState($input12,$GUI_HIDE) GUICtrlSetState($label9,$GUI_HIDE) GUICtrlSetState($label10,$GUI_HIDE) GUICtrlSetState($label11,$GUI_HIDE) GUICtrlSetState($label12,$GUI_HIDE) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $add = 0 While 1     $nMsg = GUIGetMsg()     Switch $nMsg         Case $GUI_EVENT_CLOSE             Exit         Case $Button1             $input1 = "$" & GUICtrlRead($input1)             $input2 = "$" & GUICtrlRead($input2)             $input3 = "$" & GUICtrlRead($input3)             $input4 = "$" & GUICtrlRead($input4)             $input5 = "$" & GUICtrlRead($input5)             $input6 = "$" & GUICtrlRead($input6)             $input7 = "$" & GUICtrlRead($input7)             $input8 = "$" & GUICtrlRead($input8)             If $add = 1 Then                 $input9 = "$" & GUICtrlRead($input9)                 $input10 = "$" & GUICtrlRead($input10)                 $input11 = "$" & GUICtrlRead($input11)                 $input12 = "$" & GUICtrlRead($input12)             EndIf             GUIDelete($form1)             replace()             Exit         Case $button2             Guictrlsetstate($label9,$GUI_SHOW)             GUICtrlSetState($input10,$GUI_SHOW)             GUICtrlSetState($label11,$GUI_SHOW)             GUICtrlSetState($label12,$GUI_SHOW)             GUICtrlSetState($input9,$GUI_SHOW)             GUICtrlSetState($input12,$GUI_show)             GUICtrlSetState($input11,$GUI_SHOW)             GUICtrlSetState($label10,$GUI_SHOW)             GUICtrlSetPos($button1,176,354)             GUICtrlSetState($button2,$GUI_HIDE)             $add = 1     EndSwitch WEnd Func replace ()     $code = CliPGet()     If @error Then         ToolTip ("No text in clipboard")     ElseIf IsString($code) Then         MsgBox(0, "Before", $code) ; <<<<<<<<<<<<<<<<<<<<<<<<<<         If StringLen($input1) > 1 Then $code = StringRegExpReplace($code, "(?i)(\" & $input1 & "\b)", "\" & $input2)         If StringLen($input3) > 1 Then $code = StringRegExpReplace($code, "(?i)(\" & $input3 & "\b)", "\" & $input4)         If StringLen($input5) > 1 Then $code = StringRegExpReplace($code, "(?i)(\" & $input5 & "\b)", "\" & $input6)         If StringLen($input7) > 1 Then $code = StringRegExpReplace($code, "(?i)(\" & $input7 & "\b)", "\" & $input8)         If $add = 1 Then             If StringLen($input9) > 1 Then $code = StringRegExpReplace($code, "(?i)(\" & $input9 & "\b)", "\" & $input10)             If StringLen($input11) > 1 Then $code = StringRegExpReplace($code, "(?i)(\" & $input11 & "\b)", "\" & $input12)         EndIf                 MsgBox(0, "After", $code) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<         ClipPut($code)     EndIf EndFunc  

You can see I added a "\b" which looks for a word boundary - and removed some of the pattern. You can remove the <<<<<<<<<< lines when you are happy.

SRE patterns are probably the hardest thing I have ever had to try to learn in programming (although I am sure there are far harder things out there!) and I am far from confident with creating them - so do not get too discouraged! :)

I hope it helps.

M23
0

#6 User is online   Authenticity 

  • Mass Spammer!
  • Icon
  • Group: AutoIt MVPs(MVP)
  • Posts: 2,378
  • Joined: 22-January 09
  • Gender:Male

Posted 07 November 2009 - 07:25 PM

Yeah... pattern was wrong:

[ autoIt ]    ( Popup )
If StringLen($input1) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(?:^|\b)\Q" & $input1 & "\E(?:\b|$)", "\" & $input2) If StringLen($input6) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(?:^|\b)\Q" & $input6 & "\E(?:\b|$)", "\" & $input3) If StringLen($input7) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(?:^|\b)\Q" & $input7 & "\E(?:\b|$)", "\" & $input4) If StringLen($input8) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(?:^|\b)\Q" & $input8 & "\E(?:\b|$)", "\" & $input5) If StringLen($input9) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(?:^|\b)\Q" & $input9 & "\E(?:\b|$)", "\" & $input10) If StringLen($input11) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(?:^|\b)\Q" & $input11 & "\E(?:\b|$)", "\" & $input12)

0

#7 User is offline   GEOSoft 

  • Mr. Nice Guy [ NOT!]
  • Icon
  • Group: AutoIt MVPs(MVP)
  • Posts: 6,486
  • Joined: 08-December 03
  • Gender:Male
  • Location:Nanaimo, BC, Canada

Posted 07 November 2009 - 10:26 PM

View PostAuthenticity, on 07 November 2009 - 10:25 AM, said:

Yeah... pattern was wrong:

[ autoIt ]    ( Popup )
If StringLen($input1) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(?:^|\b)\Q" & $input1 & "\E(?:\b|$)", "\" & $input2) If StringLen($input6) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(?:^|\b)\Q" & $input6 & "\E(?:\b|$)", "\" & $input3) If StringLen($input7) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(?:^|\b)\Q" & $input7 & "\E(?:\b|$)", "\" & $input4) If StringLen($input8) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(?:^|\b)\Q" & $input8 & "\E(?:\b|$)", "\" & $input5) If StringLen($input9) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(?:^|\b)\Q" & $input9 & "\E(?:\b|$)", "\" & $input10) If StringLen($input11) > 1 Then $code = StringRegExpReplace($code, "(?m)(?i)(?:^|\b)\Q" & $input11 & "\E(?:\b|$)", "\" & $input12)

Well done my good man.
That can probably be put into a loop too. Using For/Next Step 2 with simple GUICtrlRead()
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users