Sign in to follow this  
Followers 0
buymeapc

RegExp - How To Replace Every Other Quote Found?

8 posts in this topic

#1 ·  Posted (edited)

Hi all,

So, I have strings where I'm trying to delimit each value, that's currently separated with spaces, with a pipe. Here are a few of the strings:

"webstratauthentication\enableuserpasswordencryption.sql"02/23/2011  01:17:22 PM         1401  "12312312312312312""A"           "b04ecbc4-e0e2-c14b-d780-656d348b0513"  "\\nas123456n\ct40_packaging\builds\product\productname\hss\authentication\enableuserpasswordencryption.sql"
        "webstratauthentication\enableuserpasswordencryption.sql"02/23/2011  01:17:22 PM         1401  ""                 "A"           "b04ecbc4-e0e2-c14b-d780-656d348b0513"  "\\nas123456n\ct40_packaging\builds\product\productname\hss\authentication\enableuserpasswordencryption.sql"
        "ikernel.dll"                                           04/26/2002  07:48:38 PM            0  ""                 "A"           "d98c1dd4-008f-04b2-e980-0998ecf8427e"  "c:\program files\installshield 10.5\support\build\ikernel.dll"

I'd like to return the strings looking like this:

"webstratauthentication\enableuserpasswordencryption.sql"|02/23/2011|01:17:22 PM|1401|"12312312312312312"|"A"|"b04ecbc4-e0e2-c14b-d780-656d348b0513"|"\\nas123456n\ct40_packaging\builds\product\productname\hss\authentication\enableuserpasswordencryption.sql"
"webstratauthentication\enableuserpasswordencryption.sql"|02/23/2011|01:17:22 PM|1401|""|"A"|"b04ecbc4-e0e2-c14b-d780-656d348b0513"|"\\nas123456n\ct40_packaging\builds\product\productname\hss\authentication\enableuserpasswordencryption.sql"
"ikernel.dll"|04/26/2002|07:48:38 PM|0|""|"A"|"d98c1dd4-008f-04b2-e980-0998ecf8427e"|"c:\program files\installshield 10.5\support\build\ikernel.dll"

I've tried replacing the double spaces with pipes, but that messes up the first string and leaves "12312312312312312""A" without adding the pipe between the two values.

$sLine = StringRegExpReplace($sLine, "[ ]{2,}", "|")

So, I concocted a way to do it with a string-based approach.

; Strip the leading and trailing ws
$sTempLine = StringStripWS($sTempLine, 1 + 2)
; Replace all double spaces with pipes
$sTempLine = StringRegExpReplace($sTempLine, "[ ]{2,}", "|")
If StringInStr($sTempLine, '""A"') > 0 Then
    $sTempLine = StringReplace($sTempLine, '""A"', '"|"A"')
EndIf
; And check if the file name needs a pipe after its second double quote
$iPos = StringInStr($sTempLine, '"', 0, 2)
If StringMid($sTempLine, $iPos, 2) <> '"|' Then
    $sTempLine1 = StringMid($sTempLine, 1, $iPos)
    $sTempLine1 &= "|" & StringMid($sTempLine, ($iPos + 1), (StringLen($sTempLine) - $iPos))
    $sTempLine1 = StringReplace($sTempLine1, '" "', '"|"')
    $sTempLine = $sTempLine1
EndIf
ConsoleWrite("-> " & $sTempLine & @CRLF)

Is there a better regex that I can use to separate these values in the strings with pipes? I thought that it might be easiest to just put a pipe after every second quote...

Thank you for the help.

Edited by buymeapc

Share this post


Link to post
Share on other sites



One of the problems with your text is that there aren't any spaces between "12312312312312312" and "A", or between "webstratauthenticationenableuserpasswordencryption.sql" and 02/23/2011. You might have to do it with 2 passes, as I don't know that a single one will work.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Answering the question in the title. You don't need a regular expression for this. You can use StringSplit() with quote as delimiter. Loop through the array and reinsert quote at the start of every other element.

Share this post


Link to post
Share on other sites

Ok, I found a way to do it through string functions. It works, but it's just not as magical as a regex :idiot:

I was always under the impression that using regex would work much faster than the below code.

#include <String.au3>
Dim $aLine[3], $aGood[3]
$aLine[0] = '       "webstratauthentication\enableuserpasswordencryption.sql"02/23/2011  01:17:22 PM         1401  "12312312312312312""A"           "b04ecbc4-e0e2-c14b-d780-656d348b0513"  "\\nas123456n\ct40_packaging\builds\product\productname\hss\authentication\enableuserpasswordencryption.sql"'
$aLine[1] = '       "webstratauthentication\enableuserpasswordencryption.sql"02/23/2011  01:17:22 PM         1401  ""                 "A"           "b04ecbc4-e0e2-c14b-d780-656d348b0513"  "\\nas123456n\ct40_packaging\builds\product\productname\hss\authentication\enableuserpasswordencryption.sql"'
$aLine[2] = '       "ikernel.dll"                                           04/26/2002  07:48:38 PM            0  ""                 "A"           "d98c1dd4-008f-04b2-e980-0998ecf8427e"  "c:\program files\installshield 10.5\support\build\ikernel.dll"'
$aGood[0] = '"webstratauthentication\enableuserpasswordencryption.sql"|02/23/2011|01:17:22 PM|1401|"12312312312312312"|"A"|"b04ecbc4-e0e2-c14b-d780-656d348b0513"|"\\nas123456n\ct40_packaging\builds\product\productname\hss\authentication\enableuserpasswordencryption.sql"'
$aGood[1] = '"webstratauthentication\enableuserpasswordencryption.sql"|02/23/2011|01:17:22 PM|1401|""|"A"|"b04ecbc4-e0e2-c14b-d780-656d348b0513"|"\\nas123456n\ct40_packaging\builds\product\productname\hss\authentication\enableuserpasswordencryption.sql"'
$aGood[2] = '"ikernel.dll"|04/26/2002|07:48:38 PM|0|""|"A"|"d98c1dd4-008f-04b2-e980-0998ecf8427e"|"c:\program files\installshield 10.5\support\build\ikernel.dll"'

For $i = 0 To UBound($aLine) - 1
    $b = False
    $aLine[$i] = StringStripWS($aLine[$i], 1 + 2)
    $aLine[$i] = StringRegExpReplace($aLine[$i], "[ ]{2,}", "|")
    For $j = 1 To StringLen($aLine[$i])
        If StringMid($aLine[$i], $j, 1) = '"' And $b = False Then
            $b = True
        ElseIf StringMid($aLine[$i], $j, 1) = '"' And $b = True Then
            $b = False
            If StringMid($aLine[$i], ($j + 1), 1) <> '|' Then
                ConsoleWrite("Before (" & $j &"): " & $aLine[$i] & @CRLF)
                If $j <> StringLen($aLine[$i]) Then $aLine[$i] = _StringInsert($aLine[$i], '|', $j)
                ConsoleWrite("After  (" & $j &"): " & $aLine[$i] & @CRLF)
            EndIf
        EndIf
    Next
    If $aLine[$i] <> $aGood[$i] Then
        ConsoleWrite("! " &$aLine[$i] & @CRLF)
    Else
        ConsoleWrite("+ " &$aLine[$i] & @CRLF)
    EndIf
Next

Share this post


Link to post
Share on other sites

$sData = StringTrimRight(StringRegExpReplace($sData, '\h*+((?:[^"\h]|"[^"]*")+)\h*+', "$1|"), 1)
is just a quick example, certainly pattern can be done better

Ciao.


apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Share this post


Link to post
Share on other sites

RegExp is slow, slower than most string functions, but invaluable when you have something tricky to split up/find. Practically useless when the data isn't consistent though. Best advice, learn when NOT to use RegExp.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

not true, is completely contrary, today without RegExp, will be unable to do anything, or better to say the ratio of the speed is 1-1000 if you check the files 1-10 Mb, not to mention other after etc etc etc, even users that working in C++11 (Machine Code ehhhh), which (regarding the speed) can certainly do without RegEx, they too today thank RegEx, because thanks to the RegEx also in C++ itself performance have grown much

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.12.0
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

Local $sData, $fTimerDiff, $iReturn
$sData = "shfjsdjhsdhdsdudddbfHJYSJ24472727637addddddjkdaaDJDDJDDGDGL|DEUEIEUY"

$fTimerDiff = TimerInit()
For $i = 1 To 1000000
    $iReturn = StringInStr($sData, "|")
Next
$fTimerDiff = TimerDiff($fTimerDiff)
ConsoleWrite("StringInStr TimerDiff - " & $fTimerDiff & @Lf)

$fTimerDiff = TimerInit()
For $i = 1 To 1000000
    $iReturn = StringRegExp($sData, "(?i)\|")
Next
$fTimerDiff = TimerDiff($fTimerDiff)
ConsoleWrite("StringRegExp TimerDiff - " & $fTimerDiff & @Lf)
;~ >Running AU3Check (3.3.13.0)  from:C:\Program Files (x86)\AutoIt3  input:C:\Users\DXRW4E\Desktop\New AutoIt v3 Script (2).au3
;~ +>21:47:54 AU3Check ended.rc:0
;~ >Running:(3.3.12.0):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\DXRW4E\Desktop\New AutoIt v3 Script (2).au3"    
;~ --> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop
;~ StringInStr TimerDiff - 9762.88568999148
;~ StringRegExp TimerDiff - 6907.45375393011
;~ +>21:48:10 AutoIt3.exe ended.rc:0
;~ +>21:48:10 AutoIt3Wrapper Finished.
;~ >Exit code: 0    Time: 17.02
Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Share this post


Link to post
Share on other sites

Using 2 passes is much easier  :)

#include <String.au3>
Dim $aLine[3], $aGood[3]
$aLine[0] = '       "webstratauthentication\enableuserpasswordencryption.sql"02/23/2011  01:17:22 PM         1401  "12312312312312312""A"           "b04ecbc4-e0e2-c14b-d780-656d348b0513"  "\\nas123456n\ct40_packaging\builds\product\productname\hss\authentication\enableuserpasswordencryption.sql"'
$aLine[1] = '       "webstratauthentication\enableuserpasswordencryption.sql"02/23/2011  01:17:22 PM         1401  ""                 "A"           "b04ecbc4-e0e2-c14b-d780-656d348b0513"  "\\nas123456n\ct40_packaging\builds\product\productname\hss\authentication\enableuserpasswordencryption.sql"'
$aLine[2] = '       "ikernel.dll"                                           04/26/2002  07:48:38 PM            0  ""                 "A"           "d98c1dd4-008f-04b2-e980-0998ecf8427e"  "c:\program files\installshield 10.5\support\build\ikernel.dll"'
$aGood[0] = '"webstratauthentication\enableuserpasswordencryption.sql"|02/23/2011|01:17:22 PM|1401|"12312312312312312"|"A"|"b04ecbc4-e0e2-c14b-d780-656d348b0513"|"\\nas123456n\ct40_packaging\builds\product\productname\hss\authentication\enableuserpasswordencryption.sql"'
$aGood[1] = '"webstratauthentication\enableuserpasswordencryption.sql"|02/23/2011|01:17:22 PM|1401|""|"A"|"b04ecbc4-e0e2-c14b-d780-656d348b0513"|"\\nas123456n\ct40_packaging\builds\product\productname\hss\authentication\enableuserpasswordencryption.sql"'
$aGood[2] = '"ikernel.dll"|04/26/2002|07:48:38 PM|0|""|"A"|"d98c1dd4-008f-04b2-e980-0998ecf8427e"|"c:\program files\installshield 10.5\support\build\ikernel.dll"'

For $i = 0 To UBound($aLine) - 1
    $aLine[$i] = StringTrimRight(StringTrimLeft(StringRegExpReplace(StringRegExpReplace($aLine[$i], '\h{2,}', "|"), '(?<!["|])"(?!\|)', '"|'), 1), 1)

    If $aLine[$i] <> $aGood[$i] Then
        ConsoleWrite("! " &$aLine[$i] & @CRLF)
    Else
        ConsoleWrite("+ " &$aLine[$i] & @CRLF)
    EndIf
Next

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

  • Similar Content

    • Robinson1
      By Robinson1
      Well the plan is to use the power of regular expressions engine of AutoIT for patching binary data.
      Something like this: StringRegExp( $BinaryData,  "(?s)\x55\x8B.."
       
      <cut> ... Okay straight to question/problem
      ... certain bytes that are in the range from 0x80 to 0xA0 won't match.
      Hmm seem to be a char encoding problem. In detail these are 27 chars: 0x80, 0x82~8C, 0x8E, 0x91~9C, 0x9E,0x9F
      Here's a small code snippet to explore / explain this problem:
      #include "StringConstants.au3" $TestData = BinaryToString("0x7E7F808182") ;Okay $match = StringRegExp( $TestData ,'\x7E' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Okay $match = StringRegExp( $TestData ,'\x7F' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Error no match $match = StringRegExp( $TestData ,'\x80' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Okay $match = StringRegExp( $TestData ,'\x81' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Error no match $match = StringRegExp( $TestData ,'\x82' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;~ output: ;~ @extended = 2 $match = ;~ @extended = 3 $match = ;~ @extended = 0 $match = 1 ;~ @extended = 5 $match = ;~ @extended = 0 $match = 1 Hmm what to do? Go back and use the 'numberstring monster' implementation or just omit that range of 'unsafe bytes'. What is the root of this problem?
      Any idea how to fix this?
       
      Update: Okay I know a byte is not a character.
      But StringRegExp operates on String and so character level.
      Okay as long as you stay at Ansi encoding and only use /x00 - /X7F in the search pattern using  StringRegExp works well to search for binary data.
      What bytes can be matched that are in the range from /X7F - /xFF is also depending on the code page.
      So this avoid to search for bytes in the range from 0x80-0xa0 only applies to Germany.
      I just change this country setting:

      to Thai and now near all bytes from /X7F - /xFF fails to match.
    • RichardL
      By RichardL
      Text in a file, read into var with fileread:
      <> <> <> <> < J please look > <> <> <> Hi, 
      I want  a RegExp to select around 'please', back to the previous < and forward to the next >.  I can select the line of text.  Then I add in (?s) and it selects the whole text.  I think I want to make it not greedy, (?U) , that seems to make it ungreedy after, but it still selects all the previous lines.
      $sPattern = "(?s)<.*please.*>" ; 1 $sPattern = "(?s)<(?U).*please.*>" ; 2 $sPattern = "(?s)<(?U).*please(?U).*>" ; 3 $sAry = StringRegExp($sHTML, $sPattern, 3)  
    • JohnNash
      By JohnNash
      I want to rename every new instance of notepad to notepad(random number)
      If I use WinSetTitle ( "notepad", "", "notepad("&$randomnumber&")" )
      this will work pretty good, because if more windows match the search entry it will take the newest. But what if this code runs, but there is no new instance of notepad. It will rename one that was already assigned a number. So I would like to check whether it is already renamed. For example by excluding titles that contain a ")".
      How do I do that. 
      Read this, but that is pretty confusing: http://stackoverflow.com/questions/406230/regular-expression-to-match-line-that-doesnt-contain-a-word?rq=1
    • Mingre
      By Mingre
      #include <Array.au3> ; Script Start - Add your code below here Local $test = "<li>One<li>Inner<li>Innermost</li></li></li>" & _ "<li>Two</li> " $loob = StringRegExp($test, '\Q<li>\E(.*?)\Q</li>\E', 3) _ArrayDisplay($loob, "How to return the One... and Two?") Hello, can somebody help me:
      (1) How can I have the regexp matched the two outermost bullets? Such that:
       
      (2) How can I match the "Innermost" bullet?
      Thanks so much.
    • mLipok
      By mLipok
      #include <Array.au3> If @Compiled Then Exit Global Enum $FUNC_OUTER, $FUNC_NAME, $FUNC_PARAM, $FUNC_INNER _Example() Func _Example() Local $sIncludeDir = StringTrimRight(@AutoItExe, StringLen('AutoIt3.exe')) & 'Include\' Local $aOuterArray = _GetFunctionsToArray($sIncludeDir & 'Color.au3') If Not @error Then For $iOuter_idx = 0 To UBound($aOuterArray) - 1 _ArrayDisplay($aOuterArray[$iOuter_idx], ($aOuterArray[$iOuter_idx])[$FUNC_NAME]) Next EndIf EndFunc ;==>_Example Func _GetFunctionsToArray($sUDF_FileFullPath) Local $sUDFContent = FileRead($sUDF_FileFullPath) Local $aResult = StringRegExp($sUDFContent, '(?is)\RFunc (.*?)\((.*?)\)\v\R(.*?)\REndFunc', $STR_REGEXPARRAYGLOBALFULLMATCH) Return SetError(@error, @extended, $aResult) EndFunc ;==>_GetFunctionsToArray