Jump to content

Remove first and last character of line


Recommended Posts

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.

Link to comment
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

Link to comment
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.

Link to comment
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!"

Link to comment
Share on other sites

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

Link to comment
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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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