Jump to content

remove all instances of string from comma seperated value


gcue
 Share

Recommended Posts

hello world!

i am trying to remove all instances of $name from $string - any help is appreciated!

#include <Array.au3>
$msg_normal = 262144


$name = "gsh"
$string = "gsh,as,43,gsh,adf,gsh"

$final = StringRegExpReplace($string, "(.*)," & $name & ",(.*)", "$1")

debug($final)

Func Debug($variable1 = "", $variable2 = "", $variable3 = "")
;~  #include <array.au3>
;~  $msg_normal = 0

If IsArray($variable1) Then
_ArrayDisplay($variable1)
Else
If $variable2 <> "" Then
$variable1 &= @CRLF & $variable2
EndIf

If $variable3 <> "" Then
$variable1 &= @CRLF & $variable3
EndIf

ClipPut($variable1)
MsgBox($msg_normal, "Debug", $variable1)
EndIf
EndFunc   ;==>Debug
 
Link to comment
Share on other sites

  • Moderators

StringReplace seems to be easy enough for such a simple task.

$name = "gsh"
$string = "gsh,as,43,gsh,adf,gsh"

$final = StringReplace($string, $name, "")

MsgBox(0, "", $final)

"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

If it is only one string, I would stick to using Array functions, or use StringReplace for that string and for any multiple commas plus trailing and leading commas. Or you could rebuild the main string, after using StringSplit.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Or following what JLogan.... wrote, just do StringReplace twice ... once with the $name that includes a leading comma, and then again with the $name having a trailing comma. If you do it twice, you don't miss any end of string instances.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Try this.

$final = StringRegExpReplace($string, $name & ",|," & $name, "$1")

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

If it is only one string, I would stick to using Array functions, or use StringReplace for that string and for any multiple commas plus trailing and leading commas. Or you could rebuild the main string, after using StringSplit.

yea that was my original approach but after 7 or 8 lines into it i figured there has to be a better way - stringregexp! 

Link to comment
Share on other sites

Or following what JLogan.... wrote, just do StringReplace twice ... once with the $name that includes a leading comma, and then again with the $name having a trailing comma. If you do it twice, you don't miss any end of string instances.

yea i thought of that too but theres a bug with that as shown in the case below ("kgsh" would be reduced to "k")

$name = "gsh"

$string = "gsh,asdk,eas,kgsh,ake,gsh"
Link to comment
Share on other sites

That will work as long as the target string contains strings other than the replacement string...

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

$final = StringRegExpReplace($string, $name & ",|," & $name, "$1")

MsgBox(0, "", $final)

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

Try this then.

$name = "gsh"
;~ $string = "gsh,as,43,gsh,adf,gsh,gsh"
$string = "gsh,gsh"
$final = StringRegExpReplace($string, $name & ",|," & $name & "|" & $name, "")
MsgBox(0, "", $final)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

If $name contains some special regex caracters ( "[", ".", "+", "$" ...), you can use "Q" & $name & "E"
Here is mine :

$final = StringRegExpReplace($string, "(,)?\Q" & $name & "\E(?(1)|(?:,|$))", "")
Link to comment
Share on other sites

jguinch,

Try this...

$name = "gsh"
$string = "gsh,as,43,abgsh,adf,gsh,gsh"
;$string = "gsh,gsh"
$final = StringRegExpReplace($string, "(,)?\Q" & $name & "\E(?(1)|(?:,|$))", "")
MsgBox(0, "", $final)

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

I don't understand why this pattern does NOT work...

$name = "gsh"
$string = "gsh,as,43,abgsh,adf,gsh,gsh"
;$string = "gsh,gsh"
$final = StringRegExpReplace($string, "(^|,)\Q" & $name & "\E(?:,|$)", "")
MsgBox(0, "", $final)

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

This one seems to work...

$name = "gsh"
;$string = "gsh,as,43,abgsh,adf,gsh,gsh"
$string = "gsh,gsh"
$final = StringRegExpReplace($string, "(,)?\b\Q" & $name & "\E\b(,)?", "")
MsgBox(0, "", $final)

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

what if the name/string look like this?

$name = "gsh: kae,as"


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

i tried using your examples by rreplacing the comma with || but i could not get it to work

Link to comment
Share on other sites

what is the desired result of post #17?

all seperators removed?  just commas removed?  just pipes that are only preceded by the name specified?

Edited by boththose

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

Link to comment
Share on other sites

$name = "gsh: kae,as"

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

 

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

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

thanks in advance

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