millisys Posted May 6, 2011 Posted May 6, 2011 Hello programmers, embarassing question to ask, but I cannot figure out how to do do some StringRegExpReplace tasks. I have tried looking in the forums.1] Remove first and last character of a line, for example - the underscores in the following sentence "_early bird_"I have tried using the expressions "[\z\w]" and "[\Z\w]" without success.2] Remove a single character, for example - removing the "b" in the following sentence "early b bird"I have tried using the "[.]" expression for single characters without success.Thank you for your help.
enaiman Posted May 6, 2011 Posted May 6, 2011 1. You can do that without employing StringRegExp - just use StringTrimLeft and StringTrimRight 2. You need to specify the number of chars ".{1}" SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :)
millisys Posted May 6, 2011 Author Posted May 6, 2011 On 5/6/2011 at 2:15 AM, 'enaiman said: 1. You can do that without employing StringRegExp - just use StringTrimLeft and StringTrimRight 2. You need to specify the number of chars ".{1}" Hi einman, I've been to Hobart as well as Launceston and Bicheno. Tasmania is an awesome island. I tried the following but it didn't work. What am I doing wrong? StringRegExpReplace("early b bird","[.{1}]", "") Thanks
enaiman Posted May 6, 2011 Posted May 6, 2011 (edited) This will work: StringRegExpReplace("early b bird", "\s.{1}\s", " ") You need to match the spaces before and after the letter too. Glad you liked Tasmania Edited May 6, 2011 by enaiman SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :)
millisys Posted May 6, 2011 Author Posted May 6, 2011 Cool, thank you so much. I can't believe I forgot about the StringTrimRight/Left functions.Speaking of the \s expression. I have been struggling with removing multiple spaces using one StringRegExpReplace line. For example reducing occurences of 2 or more adjacent spaces to one space.Thanks.
GEOSoft Posted May 6, 2011 Posted May 6, 2011 You could also use StringMid() to advantage when you only want to remove first and last. $sStr = StringMid($sStr, 2, StringLen($sStr) -2) George Reveal hidden contents Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!"
GEOSoft Posted May 6, 2011 Posted May 6, 2011 (edited) On 5/6/2011 at 3:14 AM, 'millisys said: I have been struggling with removing multiple spaces using one StringRegExpReplace line. For example reducing occurences of 2 or more adjacent spaces to one space. Thanks. Something wrong with $sStr = StringStripWS($sStr, 4) or possibly better yet $sStr = StringStripWS($sStr, 7)? To do it with a RegEx it would be $sStr = StringRegExpReplace($sStr, "\h+", " ") EDIT: Regular Expressions are a very powerfull tool indeed but you have to learn when to use them. In both cases that you brought up, simple Sting manipulation with the String*() functions would work just fine. If you just want to play with and learn SREs then try the PCRE Toolkit in my Sig. Edited May 6, 2011 by GEOSoft George Reveal hidden contents Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!"
millisys Posted May 6, 2011 Author Posted May 6, 2011 Thanks GEOSoft, Exactly like you said, I started using the StringRegExp and tried to use it for everything and overlooked StringStripWS. Awesome!
millisys Posted May 6, 2011 Author Posted May 6, 2011 Would the following be the best way to remove a single letter ['L' in this case] at the start of a line or is there something better? $text = "L TR" if StringMid($text, 2, 1) = " " then $text = StringTrimLeft($text, 1)
GEOSoft Posted May 6, 2011 Posted May 6, 2011 That will do it. George Reveal hidden contents Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!"
GEOSoft Posted May 8, 2011 Posted May 8, 2011 Glad to help. We are here to help those that make an effort before posting in the forum which is very unlike someone who PMed me for help today even after I had given him some good advice in his thread. George Reveal hidden contents Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!"
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