Jump to content

Recommended Posts

Posted (edited)
  Quote

desired output (removing all instances of $name from $string)

 

du2: ad,43|gshy: 3d3,adf

 

You want more than just $name then, as 3 of the 4 pipes are gone as well?  so removes name and trailing separators first, and then the second pass cleans up the end where you want the preceding separator and name removed.

$string = "gsh: kae,as|du2: ad,43|gsh: kae,as|gshy: 3d3,adf|gsh: kae,as"
$name = "gsh: kae,as"
$sep="|"

$stripped = stringreplace($string , $name & $sep, "")
$stripped = stringreplace($stripped , $sep & $name , "")


msgbox(0, '' , $stripped)
Edited by boththose

  Reveal hidden contents

Posted
  On 12/30/2014 at 5:25 PM, jguinch said:

 

If the separator is not the same, you should change it in the regex, or use a variable for this.

; $name = "gsh"
; $string = "gsh,as,43,abgsh,adf,gsh,gsh"
; $separator = ","

$name = "gsh: kae,as"
$string = "gsh: kae,as|du2: ad,43|gsh: kae,as|gshy: 3d3,adf|gsh: kae,as"
$separator = "|"

$final = StringRegExpReplace($string, "(?:(\Q" & $separator & "\E)(\Q" & $name & "\E)(?=(?1)|\Z))|(?:\A(?2)(?1)|\Z)", "")
MsgBox(0, "", $final)

works great!! thank you very much

Posted
  On 12/30/2014 at 5:32 PM, boththose said:

 

You want more than just $name then, as 3 of the 4 pipes are gone as well?  so removes name and trailing separators first, and then the second pass cleans up the end where you want the preceding separator and name removed.

$string = "gsh: kae,as|du2: ad,43|gsh: kae,as|gshy: 3d3,adf|gsh: kae,as"
$name = "gsh: kae,as"
$sep="|"

$stripped = stringreplace($string , $name & $sep, "")
$stripped = stringreplace($stripped , $sep & $name , "")


msgbox(0, '' , $stripped)

only problem with that is if the name is slightly different at the beginning or end

Posted (edited)

BrewManNH extended  :)

$name = "gsh: kae,as"
$string = "gsh: kae,as|du2: ad,43|gsh: kae,as|gshy: 3d3,adf|gsh: kae,as"
$separator = "|"

$final = StringRegExpReplace($string, _ 
        '(\Q' & $name & $separator & '\E?)|(\Q' & $separator & '\E?\Q' & $name & '\E)', "")
MsgBox(0, "", $final)
Edited by mikell
Posted (edited)

@mikell - That pattern fails on...

$name = "gsh: kae,as"
$string = "gsh: kae,as|du2: ad,43|gsh: kae,asx|gshy: 3d3,adf|gsh: kae,as"
$separator = "|"

$final = StringRegExpReplace($string, _
        '(\Q' & $name & $separator & '\E?)|(\Q' & $separator & '\E?\Q' & $name & '\E)', "")
MsgBox(0, "", $final)

@gcue - You're requirements are a long way from where we started, please define the complete strings that you are working with...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

  • Moderators
Posted
  On 12/30/2014 at 6:32 PM, kylomas said:

@gcue - You're requirements are a long way from where we started, please define the complete strings that you are working with...

 

Amen, you have us floundering about here as your requirements change. Help us help you and provide exactly what you're looking for please!

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Posted (edited)
  On 12/30/2014 at 6:32 PM, kylomas said:

@gcue - You're requirements are a long way from where we started, please define the complete strings that you are working with...

 

Sooo obvious... it's the usual trouble in most questions concerning regex  :(

Edit

BTW jguinch's regex in post #20 still works and seems the best (the only..) way

Edited by mikell
Posted
  Quote

BTW jguinch's regex in post #20 still works and seems the best (the only..) way

 

Yes, and now I'm going to spend the next couple hours trying to figure it out...

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Posted (edited)

it may be the best, but it is not the parenthetical only.  #21 absolutely returns the desired output from the given input (that the input has undisclosed variations is not the scripts fault).

Edited by boththose

  Reveal hidden contents

Posted

Yes, but it fails on kylomas' variation from #25

Anyway when it becomes really tricky with hazardous and variable requirements then the most reliable way remains StringSplit + ArrayDelete + ArrayToString  ^^

Posted

ahhh, i see now, variations in the string not the name.  And for sure if there were any amount of those I would totally go the stringsplit route.  Or go learn RegEx like JG.

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