Jump to content

RegExp - Remove Leading/Trailing char


Recommended Posts

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!

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

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

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   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.

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

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   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.

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