Sign in to follow this  
Followers 0
millisys

Remove first and last character of line

12 posts in this topic

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.

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 :unsure:

Edited 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 :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

You could also use StringMid() to advantage when you only want to remove first and last.

$sStr = StringMid($sStr, 2, StringLen($sStr) -2)


George

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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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 by GEOSoft

George

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

Share this post


Link to post
Share on other sites

Thanks GEOSoft,

Exactly like you said, I started using the StringRegExp and tried to use it for everything and overlooked StringStripWS.

Awesome!

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

#10 ·  Posted

That will do it.


George

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

Share this post


Link to post
Share on other sites

#11 ·  Posted

Thank you GEOSoft

Share this post


Link to post
Share on other sites

#12 ·  Posted

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

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

Share this post


Link to post
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
Sign in to follow this  
Followers 0