xcaliber13

StringRegExpReplace the nth string

10 posts in this topic

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

Share this post


Link to post
Share on other sites



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) , " "))

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

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 to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

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)

 

1 person likes this

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 here
RegExp tutorial: enough to get started
PCRE 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)

Share this post


Link to post
Share on other sites

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"))

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Agreed  ;)

Local $sOut = StringRegExpReplace ($sIn, '(?m)^([^,]*\K,){3}' , " " )

 

Edited by mikell
typo

Share this post


Link to post
Share on other sites

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)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

I went to make another example and made post #2 again, so one more vote for that...


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

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

#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 )

 

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