gcue Posted December 30, 2014 Share Posted December 30, 2014 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 More sharing options...
Moderators JLogan3o13 Posted December 30, 2014 Moderators Share Posted December 30, 2014 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 More sharing options...
TheSaint Posted December 30, 2014 Share Posted December 30, 2014 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) Link to comment Share on other sites More sharing options...
gcue Posted December 30, 2014 Author Share Posted December 30, 2014 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) yea but it leaves behind the commas =/ Link to comment Share on other sites More sharing options...
TheSaint Posted December 30, 2014 Share Posted December 30, 2014 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) Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted December 30, 2014 Moderators Share Posted December 30, 2014 Soooo, change the $name to include the comma? "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 More sharing options...
BrewManNH Posted December 30, 2014 Share Posted December 30, 2014 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 GudeHow 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 More sharing options...
gcue Posted December 30, 2014 Author Share Posted December 30, 2014 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 More sharing options...
gcue Posted December 30, 2014 Author Share Posted December 30, 2014 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 More sharing options...
gcue Posted December 30, 2014 Author Share Posted December 30, 2014 Try this. $final = StringRegExpReplace($string, $name & ",|," & $name, "$1") brilliant bewman! thank you VERY much!!! thank all of you for your help - happy holidays to you and your families Link to comment Share on other sites More sharing options...
kylomas Posted December 30, 2014 Share Posted December 30, 2014 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 More sharing options...
BrewManNH Posted December 30, 2014 Share Posted December 30, 2014 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 GudeHow 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 More sharing options...
jguinch Posted December 30, 2014 Share Posted December 30, 2014 If $name contains some special regex caracters ( "[", ".", "+", "$" ...), you can use "Q" & $name & "E" Here is mine : $final = StringRegExpReplace($string, "(,)?\Q" & $name & "\E(?(1)|(?:,|$))", "") Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
kylomas Posted December 30, 2014 Share Posted December 30, 2014 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 More sharing options...
kylomas Posted December 30, 2014 Share Posted December 30, 2014 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 More sharing options...
kylomas Posted December 30, 2014 Share Posted December 30, 2014 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 More sharing options...
gcue Posted December 30, 2014 Author Share Posted December 30, 2014 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 More sharing options...
iamtheky Posted December 30, 2014 Share Posted December 30, 2014 (edited) 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 December 30, 2014 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
gcue Posted December 30, 2014 Author Share Posted December 30, 2014 $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 More sharing options...
jguinch Posted December 30, 2014 Share Posted December 30, 2014 (edited) 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 December 30, 2014 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now