Jump to content

Recommended Posts

Posted

Hi

i Try Create Code Replace String Between sign "|"
Example String
" |A| & |B| & |C|  & |5|"

i Want Replace Just " A B C 5 "

Example :

#include <String.au3>
$String = "|A| & |B| & |C| & |5|"
local $TTT

$BETW = _StringBetween($String , "|" , "|")
For $i = 0 To UBound($BETW) - 1

If Not BitAND($i,1) Then
  $TTT = $TTT & StringReplace($String , $BETW[$i] , "Hello World !") & @CRLF
EndIf

Next
MsgBox(64 , "" , $TTT)

*This code needs correction

i write this Example , But this code Replace With String recurrence   ,  i want Replace don't recurrence  

 

 

  • Moderators
Posted

Alex1986,

Perhaps this:

$sString = "|A| & |B| & |C| & |5|"

$sNewString = StringRegExpReplace($sString, "(^\||\| & \||\|$)", "")

ConsoleWrite($sNewString & @CRLF)

The RegEx replaces initial and trailing "|" together with all the intermediate "| & |" strings.

Is that what you wanted?

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:

  Reveal hidden contents

 

Posted
  On 9/12/2015 at 7:26 PM, Melba23 said:

Alex1986,

Perhaps this:

$sString = "|A| & |B| & |C| & |5|"

$sNewString = StringRegExpReplace($sString, "(^\||\| & \||\|$)", "")

ConsoleWrite($sNewString & @CRLF)

The RegEx replaces initial and trailing "|" together with all the intermediate "| & |" strings.

Is that what you wanted?

M23

this get between , i want replace between

Befor : "|H| & |B| & |C| & |5|"

i do replace "Alex" for all

After : "|Alex| & |Alex| & |Alex| & |Alex|"

Thanks For Help

  • Moderators
Posted

Alex1986,

Then this should do the trick:

$sString = "|A| & |B| & |C| & |5|"

$sReplace = "Alex"

$sNewString = StringRegExpReplace($sString, "(^|\s)\|.\|(\s|$)", " |" & $sReplace & "| ")

ConsoleWrite($sNewString & @CRLF)

Look for a string that matches "(start or space) | (any single character) | (Space or end)" and replace it as required.

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:

  Reveal hidden contents

 

Posted
  On 9/12/2015 at 7:40 PM, jguinch said:

Or this :

$sString = "|A| & |B| & |C| & |5|"

$sNewString = StringRegExpReplace($sString, "[A-Z0-9]", "Hello World !")
ConsoleWrite($sNewString & @CRLF)

As you can see, your message is not clear enough...

this Replace Characters[A-Z]figures[0-9]  , But If found other ( * & ^ % $ # ; " ..........etc )   , does not happen properly

  • Moderators
Posted

Alex1986,

Stop changing the goalposts as the thread progresses!

How about this:

$sString = "|A&| & |B#| & |*C| & |%5|"

$sReplace = "Alex"

$sNewString = StringRegExpReplace($sString, "(?U)(^|\s)\|.+\|(\s|$)", " |" & $sReplace & "| ")

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:

  Reveal hidden contents

 

Posted

and without regexp.  This is provided this string doesnt have a bunch of bonus pipe characters thrown in.

$sString = "|A&| & |B#| & |*C| & |%5|"
$i = 1
$sMax = stringinstr($sString , "|" , 0 , -1)

Do
        $sFirst = Stringinstr($sString, "|" , 0 , $i)
    $i += 1
        $sSecond = Stringinstr($sString, "|" , 0 ,  $i)
    $i += 1
        $sString = stringreplace($sString , stringmid($sString , $sFirst + 1 , $sSecond - $sFirst - 1) , "Alex")

Until $sSecond >= $sMax

msgbox(0, '' , $sString)

 

  Reveal hidden contents

Posted
  On 9/12/2015 at 8:28 PM, boththose said:

and without regexp.  This is provided this string doesnt have a bunch of bonus pipe characters thrown in.

$sString = "|A&| & |B#| & |*C| & |%5|"
$i = 1
$sMax = stringinstr($sString , "|" , 0 , -1)

Do
        $sFirst = Stringinstr($sString, "|" , 0 , $i)
    $i += 1
        $sSecond = Stringinstr($sString, "|" , 0 ,  $i)
    $i += 1
        $sString = stringreplace($sString , stringmid($sString , $sFirst + 1 , $sSecond - $sFirst - 1) , "Alex")

Until $sSecond >= $sMax

msgbox(0, '' , $sString)

 

this best , thank you  :thumbsup:

Posted (edited)
  On 9/12/2015 at 8:28 PM, boththose said:

and without regexp.  This is provided this string doesnt have a bunch of bonus pipe characters thrown in.

$sString = "|A&| & |B#| & |*C| & |%5|"
$i = 1
$sMax = stringinstr($sString , "|" , 0 , -1)

Do
        $sFirst = Stringinstr($sString, "|" , 0 , $i)
    $i += 1
        $sSecond = Stringinstr($sString, "|" , 0 ,  $i)
    $i += 1
        $sString = stringreplace($sString , stringmid($sString , $sFirst + 1 , $sSecond - $sFirst - 1) , "Alex")

Until $sSecond >= $sMax

msgbox(0, '' , $sString)

 

 

There Error  :sweating:

$sString = "| Autoit | & | For | & | All |" ;This Not Work
;$sString = "| AA | & | BB | & | CC |" ; This Work 

$sign = "|"

$i = 1
$sMax = stringinstr($sString , $sign , 0 , -1)

Do
        $sFirst = Stringinstr($sString, $sign , 0 , $i)
    $i += 1
        $sSecond = Stringinstr($sString, $sign , 0 ,  $i)
    $i += 1
        $sString = stringreplace($sString , stringmid($sString , $sFirst + 1 , $sSecond - $sFirst - 1) , "Alex")

Until $sSecond >= $sMax

MsgBox(64 , "" , $sString)

 

Edited by Alex1986
Posted

I didnt really do any error checking.  I think checking $sSecond for a 0 return will fix the issue you found.

$sString = "| Autoit | & | For | & | All |"
$i = 1
$sMax = stringinstr($sString , "|" , 0 , -1)

Do
        $sFirst = Stringinstr($sString, "|" , 0 , $i)
    $i += 1
        $sSecond = Stringinstr($sString, "|" , 0 ,  $i)
        If $sSecond = 0 Then exitloop
    $i += 1
        $sString = stringreplace($sString , stringmid($sString , $sFirst + 1 , $sSecond - $sFirst - 1) , "Alex")
;~      msgbox(0, '' , $sString)

Until $sSecond >= $sMax

msgbox(0, '' , $sString)

 

  Reveal hidden contents

Posted (edited)

i test this code  , I don't want replace this "YY"

 

$sString = "|Y| & |Y| & YY |Y| "

$i = 1
$sMax = stringinstr($sString , "|" , 0 , -1)

Do
        $sFirst = Stringinstr($sString, "|" , 0 , $i)
    $i += 1
        $sSecond = Stringinstr($sString, "|" , 0 ,  $i)
        If $sSecond = 0 Then exitloop
    $i += 1
        $sString = stringreplace($sString , stringmid($sString , $sFirst + 1 , $sSecond - $sFirst - 1) , "Alex")
;~      msgbox(0, '' , $sString)

Until $sSecond >= $sMax

msgbox(0, '' , $sString)

 

Thank you

Edited by Alex1986
Posted (edited)

well, by making it harder, you have made it easier.  Do not fall in love with one method or the other, just pick the one that fits the job.

$sString = "|Y| & |Y| & YY |Y| "

msgbox(0, '' , stringreplace($sString , "|Y|" , "|Alex|"))

 

$sString = "|Y| & |Y| & YY |Y| "
$aString = stringsplit($sString , "|" , 2)

$sOut= ""
For $y in $aString
    If $y = "Y" then $y = "|Alex|"
    $sOut &= $y
Next

msgbox(0, '' ,$sOut)

 

Edited by boththose

  Reveal hidden contents

Posted (edited)
  On 9/12/2015 at 10:12 PM, boththose said:

well, by making it harder, you have made it easier.  Do not fall in love with one method or the other, just pick the one that fits the job.

$sString = "|Y| & |Y| & YY |Y| "

msgbox(0, '' , stringreplace($sString , "|Y|" , "|Alex|"))

 

 

i want Replace Between "|"  ,  i Don't Replace  "Yes"    

$sString = "|Y| & |Thank| & Yes |You| " ; 

$i = 1
$sMax = stringinstr($sString , "|" , 0 , -1)

Do
        $sFirst = Stringinstr($sString, "|" , 0 , $i)
    $i += 1
        $sSecond = Stringinstr($sString, "|" , 0 ,  $i)
        If $sSecond = 0 Then exitloop
    $i += 1
        $sString = stringreplace($sString , stringmid($sString , $sFirst + 1 , $sSecond - $sFirst - 1) , "Alex")


Until $sSecond >= $sMax

Msgbox(0,"",$sString)

 

 

 

Edited by Alex1986
Posted
  On 9/12/2015 at 10:28 PM, Alex1986 said:

<snip>

i want Replace Between "|"  ,  i Don't Replace  "Yes"    

<snip>

??? what's your final goal?

Local $sString = "|Y| & |Thank| & Yes |You| " ;
Local $sReplace = "Alex"

Local $array = StringSplit($sString, "|")
For $i = 1 To UBound($array) - 1
    If Not StringInStr($array[$i], "&") Then $sString = StringReplace($sString, "|" & $array[$i] & "|", "|" & $sReplace & "|")
Next
ConsoleWrite($sString & @CRLF)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted
  On 9/12/2015 at 10:28 PM, Alex1986 said:

...

...
$sMax = stringinstr($sString , "|" , 0 , -1)

Do
... 

With each loop, it is highly likely the value of $sMax  will change.  Because on each loop the length of $sString might change with each string replacement.

It would be better to have the assignment of the value to $sMax inside the loop.

 

And another example.

$sString = "|Y| & | Thank | & Yes |You| "
$sReplace = "Alex"

ConsoleWrite(_StringInsertBetween($sString, "|", "|", $sReplace) & @LF) ; Returns: |Alex| & | Alex | & Yes |Alex|

ConsoleWrite(_StringInsertBetweenEx($sString, "|", "|", $sReplace) & @LF) ; Returns: |Alex| & |Alex| & Yes |Alex|


;Conserve spaces.
Func _StringInsertBetween($sString, $sStart, $sEnd, $sInsert)
    Return StringRegExpReplace($sString, "(\Q" & $sStart & "\E\h*)(.*?)(\h*\Q" & $sEnd & "\E)", "${1}" & $sInsert & "${3}")
EndFunc   ;==>_StringInsertBetween

;Disregard spaces.
Func _StringInsertBetweenEx($sString, $sStart, $sEnd, $sInsert)
    Return StringRegExpReplace($sString, "(\Q" & $sStart & "\E)(.*?)(\Q" & $sEnd & "\E)", "${1}" & $sInsert & "${3}")
EndFunc   ;==>_StringInsertBetweenEx

 

 

Posted

indeed, it has many more problems as well.  But it pimps that one string.

  Reveal hidden contents

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...