Jump to content
Sign in to follow this  
buymeapc

RegExp - How To Replace Every Other Quote Found?

Recommended Posts

buymeapc

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
BrewManNH

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
czardas

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
buymeapc

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
DXRW4E

$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
BrewManNH

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
DXRW4E

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
mikell

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  

  • Similar Content

    • mLipok
      By mLipok
      In April 5, 2013 I ask @Lazycat 
      he answer:
      Then I change this tool a little.
      Now I back to this and make bigger changed.
      Here is new version.
      Update History: = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/07 v3.0 * Changed: AU3Check compilant - mLipok * Changed: almost all Variables renamed - mLipok * Added: "Delete RegExp Results" - mLipok * Added: support for dual monitor - mLipok * Added: "full screen mode" - mLipok = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/08 v3.1 * Added: colors for each Edit control - used GUICtrlSetBkColor() - mLipok * Added: FullScreen option (Checkbox + INI + Remarks in Tip) - mLipok * Added: _IsChecked() - mLipok * Changed: WinMove() - change size of window using: WindowWidth and WindowHeight - mLipok = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/13 v3.2 * Added: If $bFullScreen Then GUICtrlSetFont() - mLipok * Added: WM_COMMAND , $EN_CHANGE - prevent CPU overheat - mLipok = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/29 v3.3 * Changed: $_g_idCheckbox_Clear - also clear $_g_idEdit_Result - mLipok * Changed: ClearResult If GUICtrlRead($_g_idEdit_MatchText) = '' Or GUICtrlRead($_g_idEdit_MatchText) = '' - mLipok * Fixed: prevention CPU overheat - If $iGuiMsg <> 0 Then $_g_bWasAChange = True - any GUI change will fire RegExp result refresh - mLipok * Fixed: Top possition of $_g_idLabel_Dummy control - mLipok * Added: support for TabSwitch - CTRL+TAB and CTRL+SHIFT+TAB - mLipok = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  
    • MrCheese
      By MrCheese
      argh, pulling my hair out.
      considering this post: 
       
      say for a string = "03a", how can I strip out the leading 0 and the a.
      I have tried:
      $new = StringRegExpReplace($string, '[^1-9][^0-9]', '')
       
      and various combinations:
      ^0+[^0-9]
      [^[:digit:]]
      "[^0].*"
      "^0*(d+)"
       
      I'm going loopy!
       
       
    • PClough
      By PClough
      Hi everyone!
      After updating autoit, I tried to run an old program using complex regexp's.  It did not work.  Eventually I broke the problem down to this example:
       
      #include <Array.au3> $buf = "First title" & @CRLF & "Tom" & Chr(0x92) & "s sleepwalking" & @CRLF & "Last | line" & @CRLF $items = StringRegExp($buf, '([\x20-\xff]+)\x0d\x0a', 3) _ArrayDisplay($items,'') And this is the result I get when running it:
      Row 0
       
    • Miliardsto
      By Miliardsto
      Hello . How to do that
      $regexp = starts from "abcdef" and after this could be anything in name
      WinActivate($regexp)
×