xcaliber13 Posted August 17, 2016 Share Posted August 17, 2016 Hello, Once again I continue to struggle with regex statements. I have searched the forums and the internet and still cannot get this to work. I have a csv file that is comma delimited. What I am trying to do is to replace the 3rd occurrence of a comma with a space on every line. Example: FIN,MRN,LastName,FirstName,DateOfService,Mercy Dept Name,ServiceCode would become FIN,MRN,LastName FirstName,DateOfService,Mercy Dept Name,ServiceCode on every line of the file. Also should I be writing the csv file to an array first and then do the StringRegExpReplace? Any help to point me in the right direction would be great! Thank you Link to comment Share on other sites More sharing options...
iamtheky Posted August 17, 2016 Share Posted August 17, 2016 thats not really a problem for regex, but they rarely are.. $str = "FIN,MRN,LastName,FirstName,DateOfService,Mercy Dept Name,ServiceCode" MsgBox(0, '' , stringreplace($str , StringInStr($str , "," , 0 , 3) , " ")) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
funkey Posted August 17, 2016 Share Posted August 17, 2016 I would read the file into an array, then use a loop with StringInStr() and StringReplace(). I'm no RegExp user Programming today is a race between software engineers striving tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning. Link to comment Share on other sites More sharing options...
jchd Posted August 17, 2016 Share Posted August 17, 2016 Regexp: Local $sIn = _ "FIN,MRN,LastNameA,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameB,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameC,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameD,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameE,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF Local $sOut = StringRegExpReplace($sIn, "(?m)([^,]*,[^,]*,[^,]*),(.*)", "$1 $2") ConsoleWrite($sOut) iamtheky 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
iamtheky Posted August 17, 2016 Share Posted August 17, 2016 I made one, probably buggy as shit but i think it works for the ex. Local $sIn = _ "FIN,MRN,LastNameA,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameB,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameC,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameD,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameE,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF msgbox(0, 'regexpreplace' , stringregexpreplace($sIn , "((.*?,){2}.*?),(.*\r)" , "$1 $3")) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
jguinch Posted August 17, 2016 Share Posted August 17, 2016 Using \K, it seems faster : Local $sOut = StringRegExpReplace($sIn, "(?m)^[^,]*,[^,]*,[^,]*\K,", " ") Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
mikell Posted August 17, 2016 Share Posted August 17, 2016 (edited) Agreed Local $sOut = StringRegExpReplace ($sIn, '(?m)^([^,]*\K,){3}' , " " ) Edited August 17, 2016 by mikell typo Link to comment Share on other sites More sharing options...
iamtheky Posted August 17, 2016 Share Posted August 17, 2016 in case you wanted to bring array into it i would go arraytostring, its easy to read #include<array.au3> Local $sIn = _ "FIN,MRN,LastNameA,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameB,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameC,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameD,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameE,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF $aIn = stringsplit($sIn , @CRLF , 2) $sOut = "" For $i = 0 to ubound ($aIn) - 1 $sOut &= _ArrayToString(stringsplit($aIn[$i] , "," , 2) , "," , 0 , 2) & " " & _ArrayToString(stringsplit($aIn[$i] , "," , 2) , "," , 3) Next msgbox(0, '' , $sOut) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
iamtheky Posted August 17, 2016 Share Posted August 17, 2016 I went to make another example and made post #2 again, so one more vote for that... ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
mikell Posted August 18, 2016 Share Posted August 18, 2016 Good idea, but using post #2 to replace the code in post #8 which is totally awful - performance side BTW jguinch, using \K is faster indeed expandcollapse popup#include<array.au3> Local $sIn = _ "FIN,MRN,LastNameA,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameB,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameC,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameD,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF & _ "FIN,MRN,LastNameE,FirstName,DateOfService,Mercy Dept Name,ServiceCode" & @CRLF Local $hTimer = TimerInit() Local $sOut = StringRegExpReplace($sIn, "(?m)([^,]*,[^,]*,[^,]*),(.*)", "$1 $2") $fDiff0 = TimerDiff($hTimer) ;msgbox(0, '1' , $sOut) $hTimer = TimerInit() Local $sOut = StringRegExpReplace ($sIn, '(?m)^([^,]*\K,){3}' , " " ) $fDiff1 = TimerDiff($hTimer) ;msgbox(0, '1' , $sOut) $hTimer = TimerInit() $aIn = stringsplit($sIn , @CRLF , 3) $sOut = "" For $i = 0 to ubound ($aIn) - 1 $sOut &= _ArrayToString(stringsplit($aIn[$i] , "," , 2) , "," , 0 , 2) & " " & _ArrayToString(stringsplit($aIn[$i] , "," , 2) , "," , 3) & @crlf Next $fDiff2 = TimerDiff($hTimer) ;msgbox(0, '2' , $sOut) $hTimer = TimerInit() $aIn = stringsplit($sIn , @CRLF , 3) $sOut = "" For $i = 0 to ubound ($aIn) - 1 $sOut &= stringreplace($aIn[$i] , StringInStr($aIn[$i] , "," , 0 , 3) , " ") & @crlf Next $fDiff3 = TimerDiff($hTimer) ;msgbox(0, '3' , $sOut) msgbox(0, '' , "regex1 = " & $fDiff0 & @crlf & _ "regex2 = " & $fDiff1 & @crlf & _ "array = " & $fDiff2 & @crlf & _ "string = " & $fDiff3 ) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now