Jump to content
Sign in to follow this  
buymeapc

RegExp - How To Replace Every Other Quote Found?

Recommended Posts

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

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

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

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Tosyk
      Hi,
      Please help me to change metasymbol line. Right now I have this condition code:
      If StringInStr($_sName, 'TEXT ') Then $_sName = StringRegExpReplace($_sName, '(^.*)\TEXT (.*)$', '$2') $_sName = StringRegExpReplace($_sName, '(^.*)\ (.*)$', '$1') If Not CheckIsSave_($_sName) Then It work fine with this text file and finds each line which start from 'TEXT':
      Material B7E671143D244B ==================================== TEXT 2F3139D816C34D 1 TEXT B6A968EF2505A2 1 TEXT 35206697A04F91 1 TEXT EB485AF490D83D 1 TEXT 0DAB42294BD9B3 1 TEXT 3D6525BEE360E1 0 Material D6906B886B06E3 ==================================== TEXT 0CCECCCCFB62AE 1 TEXT 1E14CB29AB43F0 1 TEXT FB7F0DCE9B5950 1 But I have a new text file now the lines of which now are start with 0:, 1: and so on:
      sm_0 --------------- 0: dummy_gray 1: c_com_socksa_mt 2: c_com_socksa_tn 3: dummy_white 4: default_z 5: dummy_nmap 6: --- 7: --- sm_1 --------------- 0: c_com_prisoner_shoes_di 1: c_com_prisoner_shoes_mt 2: c_com_prisoner_shoes_tn 3: dummy_white 4: default_z 5: c_com_leatherb_rt 6: --- 7: --- how to change (or add) the condition code above to work with new text file?
      I'm trying to change this script: http://autoit-script.ru/threads/poisk-fajlov-rekursivno-po-dannomu-spisku.26970/post-148646
       
    • By seadoggie01
      I'm trying to capture everything after a "#ToDo" in my scripts. I got that like this:
      (?i)[^\v]*#todo(.*) But then I thought it would be nice to use underscores to continue the ToDo... kind of like this:
      #ToDo: This is a really long explanation about something _ # that is very in-depth and needs to take up a lot of _ # space in a ToDo comment Global $variables = "Bad" I can't seem to capture everything... and maybe I'm trying to do too much with Regex... I keep trying variations of this:
      Condensed Version: (?im)[^\v]*#todo(?:([^\v]*)_\s*)*#([^\v]*) Expanded with comments (?ixm)(?# Ignore case, ignore newlines in Regex, use multiline option)# [^\v]*(?# Match leading space/s)# \#todo(?# Match the #todo)# (?:([^\v]*)_\s*)*(?# Match lines ending with _)# \#([^\v]*)(?# Last line only, no _'s)# I never seem to be able to build an array well with Regex... I saw something once about not being able to capture repeated patterns, and I think that's my issue
    • By genius257
      Inspired by PHP's preg_split.
      Split string by a regular expression.
      Also supports the same flags as the PHP equivalent.
      v1.0.1
       
      Example:
      #include "StringRegExpSplit.au3" StringRegExpSplit('splitCamelCaseWords', '(?<=\w)(?=[A-Z])') ; ['split', 'Camel', 'Case', 'Words']  
    • By RAMzor
      Hi guys I need your help.
      I have string like this : "TDM111A5,      RCT222Y5/ 7  ; FDT444E4 /8 , ABC222R5"
      I need find a coma or semicolon and delete white spaces before and after them
      The output should be a string and/or array 
      String : "TDM111A5,RCT222Y5/ 7;FDT444E4 /8,ABC222R5"
      Array:
      TDM111A5
      RCT222Y5/ 7
      FDT444E4 /8
      ABC222R5
    • By BlueBandana
      Is there a way to output the regex matches into a file?
      I have a script to compare two files and check for regex matches.
      I want to output the matching regex of 'testexample.txt' to another file.
      #include <MsgBoxConstants.au3> #include <Array.au3> $Read = FileReadToArray("C:\Users\admin\Documents\testexample.txt") $Dictionary = FileReadToArray("C:\Users\admin\Documents\example.txt") For $p = 0 To UBound($Dictionary) - 1 Step 1 $pattern = $Dictionary[$p] For $i = 0 To UBound($Read) - 1 Step 1 $regex = $Read[$i] If StringRegExp($regex, $pattern, 0) Then MsgBox(0, "ResultsPass", "The string is in the file, highlighted strings: " ) Else MsgBox(0, "ResultsFail", "The string isn't in the file.") EndIf Next Next  
×
×
  • Create New...