HurleyShanabarger

RegExp - Remove Leading/Trailing char

6 posts in this topic

Hi guys,

 

is there an easy pattern to remove the a specific leading/trailing char from a line? In my case it is the quotation mark, that might be in the first/and or last place and as well in the middle of each line.

 

"test_line_1
test_line_2"
"test_line_3"
test"l"in"e_4"

I would like to have this as output:

test_line_1
test_line_2
test_line_3
test"l"in"e_4

That is the pattern that I have but iut won't match the last one:

StringRegExp($_lv_sString, "(?m)^["]?(.*)", 3)

Thanks!

Share this post


Link to post
Share on other sites



Hello. Maybe something like this.

Local $sString = '"test_line_1' & @CRLF & _
        'test_line_2"' & @CRLF & _
        '"test_line_3"' & @CRLF & _
        'test"l"in"e_4"' & @CRLF

$sString = StringRegExpReplace($sString, '(?m)^"|"$', "")
ConsoleWrite($sString & @CRLF)

Saludos

Share this post


Link to post
Share on other sites

Could also be like this, depending on the exactly expected result ...

StringRegExpReplace($sString, '(?m)^"|"(?=[^"]*"$)|"$', "")

 

Share this post


Link to post
Share on other sites

Hi.

Because you wanted StringRegExp:

#include <Array.au3>
Local $_lv_sString = '"test_line_1' & @CRLF & _
        'test_line_2"' & @CRLF & _
        '"test_line_3"' & @CRLF & _
        'test"l"in"e_4"' & @CRLF
Local $aArray = StringRegExp($_lv_sString, '(?m)^"*(.*\d+)', 3)
_ArrayDisplay($aArray)

Conrad


SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites

Because my post above was only some kind of work around and not exactly what you asked for (it only worked for you example but not in general) I tried a bit and here's a solution for general purpose:

#include <Array.au3>
Local $_lv_sString = '"test_line_1' & @CRLF & _
        'test_line_2"' & @CRLF & _
        '"test_line_3"' & @CRLF & _
        'test"l"in"e_4"' & @CRLF
Local $sSpecificLeadingTrailingChar = '"'
Local $sRegex = '(?m)^\' & $sSpecificLeadingTrailingChar & '?(.+?)(?:\' & $sSpecificLeadingTrailingChar & '*$)'
Local $aArray = StringRegExp($_lv_sString, $sRegex, 3)
_ArrayDisplay($aArray)

Conrad


SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites

Thanks you guys, the solution von mikell is working the best for me.

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