I came up with the following code:
Code: Select all
LBITEMS := "~Item1|Item2|Item3|~Item4|Item5|Item6|Item7|Item8|~Item9"
Msgbox % RTrim( RegExReplace( LBITEMS "|" , "~.*?\|" ), "|" )
Thanks.
Code: Select all
LBITEMS := "~Item1|Item2|Item3|~Item4|Item5|Item6|Item7|Item8|~Item9"
Msgbox % RTrim( RegExReplace( LBITEMS "|" , "~.*?\|" ), "|" )
Code: Select all
LBITEMS := "~Item1|Item2|Item3|~Item4|Item5|Item6|Item7|Item8|~Item9"
MsgBox % regExReplace( "|" LBITEMS,"U)\|~.*(\||$)")
Oops! That leaves a leading PIPE when the first item doesn't contain Tilde.Suresh wrote:The following modification to your code seems to work:
Code: Select all
LBITEMS := "~Item1|Item2|Item3|~Item4|Item5|Item6|Item7|Item8|~Item9" MsgBox % regExReplace( "|" LBITEMS,"U)\|~.*(\||$)")
Oh! Here is a sample of an actual item that I want to remove: ~micromole/decimeter³ (µmol/dm³) *Masonjar13 wrote: regExReplace(LBITEMS,"U)(~\b\w+\b\|)|(\|~\b\w+\b$)")
Provided that no items contain any other symbols, this will always match.
Code: Select all
regExReplace(LBITEMS,"U)~.*(\||$)")
Code: Select all
;overscores indicate which characters need to be removed:
;METHOD 1: probably easier:
;if first char is ~, replace from ~ to first | not followed by ~ (or to end of string)
;replace from |~ to before next | (or to end of string)
;~Item1|Item2|Item3|~Item4|Item5|Item6|Item7|Item8|~Item9
;¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯
;METHOD 2: probably harder:
;~Item1|Item2|Item3|~Item4|Item5|Item6|Item7|Item8|~Item9
;¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯
q:: ;'please release me'
LBITEMS := "~Item1|Item2|Item3|~Item4|Item5|Item6|Item7|Item8|~Item9"
LBITEMS := RegExReplace(LBITEMS, "^~.*?(\|(?!~)|$)|\|~.*?((?=\|)|(?=$))")
MsgBox, % LBITEMS
return
Code: Select all
q:: ;RegEx - test remove items that begin with ~
vNeedle := "(~.*?\|)|(\|~[^~|]+$)"
vNeedle := "U)(~[^\|]+\|)|(\|~[^\|]+$)"
vNeedle := "^~.*?(\|(?!~)|$)|\|~.*?((?=\|)|(?=$))" ;works 'so I pass the test, in any old badass MC contest'
vDoMsgBox := 1
vCountFail := 0
Loop, 512 ;2^9 = 512 = 1000000000 (bin) (9 0s)
{
vBin := JEE_Dec2Bin(A_Index-1, 9)
vTemp1 := vTemp2 := ""
Loop, Parse, vBin
{
vTemp1 .= (A_Index=1?"":"|") (A_LoopField?"":"~") "item1"
vTemp2 .= A_LoopField?((vTemp2=""?"":"|") "item1"):""
}
vTemp3 := RegExReplace(vTemp1, vNeedle)
if !(vTemp2 = vTemp3)
{
vCountFail++
if vDoMsgBox
MsgBox, % vBin "`r`n" vTemp1 "`r`n" vTemp2 "`r`n" vTemp3
}
}
MsgBox, % "fail count: " vCountFail
return
;==================================================
;where vLen is the minimum length of the number to return (i.e. pad it with zeros if necessary)
JEE_Dec2Bin(vNum, vLen=1)
{
if (SubStr(vNum, 1, 2) = "0x")
vNum += 0
if !RegExMatch(vNum, "^\d+$")
return ""
while vNum
vBin := (vNum & 1) vBin, vNum >>= 1
return Format("{:0" vLen "}", vBin)
;if (StrLen(vBin) < vLen)
; Loop, % vLen - StrLen(vBin)
; vBin := "0" vBin
;return vBin
}
;where vLen is the minimum length of the number to return (i.e. pad it with zeros if necessary)
JEE_Dec2Bin(vNum, vLen=1)
{
if (SubStr(vNum, 1, 2) = "0x")
vNum += 0
if !RegExMatch(vNum, "^\d+$")
return ""
while vNum
vBin := (vNum & 1) vBin, vNum >>= 1
return Format("{:0" vLen "}", vBin)
}
Code: Select all
LBITEMS := "~Item1|~Item2|Item3|~Item4|Item5|Item6|Item7|~Item8|~Item9"
MsgBox % RegExReplace( LBITEMS , "(~.*?\|)|(\|~[^~|]+$)" )
Code: Select all
q::
MsgBox, % RegExReplace("aaaaaa", "^aa") ;aaaa
return
Code: Select all
q::
MsgBox, % RegExReplace("aaaaaa", "\Gaa") ;(blank)
return
Users browsing this forum: Google [Bot], roeleboele and 372 guests