Jump to content

How Replace Between This String


Alex1986
 Share

Recommended Posts

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  

 

 

Link to comment
Share on other sites

  • Moderators

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:

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

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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:

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

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:

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

or this (last attempt for me) :

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

$sNewString = StringRegExpReplace($sString, "(?<=\|)\h*[^|&]+\h*(?=\|)", "Alex")
ConsoleWrite($sNewString & @CRLF)

 

Link to comment
Share on other sites

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)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

...

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

 

 

Link to comment
Share on other sites

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

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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