Jump to content

remove all instances of string from comma seperated value


gcue
 Share

Recommended Posts

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

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

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

  • Moderators

@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!

Link to comment
Share on other sites

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

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

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

Link to comment
Share on other sites

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.

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

Link to comment
Share on other sites

@kylomas : what is so hard to understand in the pattern ? Do you need explanations ?

Edit : here is a good way to understand it (I think) : https://regex101.com/r/iR0dK7/1

Edited by jguinch
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...