gruntydatsun Posted October 29, 2012 Posted October 29, 2012 (edited) Hello,I'm trying to extract values from a csv file using StringRegExp into a 1d array. I am trying to extract the values only, not capturing the commas or the word END at the end of each line.I understand basic repeating like below:$string = "123123asdlkjasdlfkjasd;lkfj321321" $array = StringRegExp($string,"(d{6})",3) _ArrayDisplay($array,"ARRAY")but this has me stumped.The file format isname,age,weight,address,ENDtim,21,75,10 Jones St,ENDrob,32,90,21 Smith St,ENDwill,65,72,5 Davis St,ENDI'm trying to use capturing and non-capturing groups with repeating characters to do it.So far I have:$string = FileRead(@scriptdir & "data.dat") $array = StringRegExp($string,"$regex = "(?:(.*?),{4})",3) _ArrayDisplay($array,"ARRAY")I know I can do it easily by just writing the regexp out longhand like (.*?),(.*?),(.*?),(.*?), but the number of fields can change and I need it to be dynamic. Sorry to be pushy about it but the dynamic amount of fields part is fairly important.I have previously done this building the longhand expression in a loop like below:for $x = 1 to $fields $regex &= "(.*?)," next $regex &= "End"Thanks for any help you can give Edited October 29, 2012 by gruntydatsun
Malkey Posted October 29, 2012 Posted October 29, 2012 Hello, I'm trying to extract values from a csv file using StringRegExp into a 1d array. I am trying to extract the values only, not capturing the commas or the word END at the end of each line. .... The file format is name,age,weight,address,END tim,21,75,10 Jones St,END rob,32,90,21 Smith St,END will,65,72,5 Davis St,END ..... This example returns what appears to be you are after. Local $sResult Local $string = FileRead(@ScriptDir & "\data.dat") Local $array = StringRegExp($string, "(.*?),", 3) For $i = 0 To UBound($array) - 1 $sResult &= "$array[" & $i & "] = " & $array[$i] & @LF Next MsgBox(0, "Results", $sResult) ConsoleWrite($sResult & @LF) #cs Returns:- $array[0] = name $array[1] = age $array[2] = weight $array[3] = address $array[4] = tim $array[5] = 21 $array[6] = 75 $array[7] = 10 Jones St $array[8] = rob $array[9] = 32 $array[10] = 90 $array[11] = 21 Smith St $array[12] = will $array[13] = 65 $array[14] = 72 $array[15] = 5 Davis St #ce
gruntydatsun Posted October 29, 2012 Author Posted October 29, 2012 (edited) Thanks Malkey for the reply. That's pretty much how I'm doing it now. I'm mainly looking for a way of using the repeating {x} when part of what's repeating is to be captured and part is not. (.*?), but then what? repeating with {x} only seems to work when you put it inside the brackets. How do I get all of (.*?), to be repeating? EDIT: just to keep my word count on "repeating" up, i'll say it again, repeating Edited October 29, 2012 by gruntydatsun
Robjong Posted October 29, 2012 Posted October 29, 2012 (edited) You are really close... "(.*?)," but this is a better way of writing it "([^,v]+)," Edited October 29, 2012 by Robjong
kylomas Posted October 29, 2012 Posted October 29, 2012 gruntydatsun, Is this the result that you are looking for? Local $sResult Local $string = 'name,age,weight,address,END' & @crlf & _ 'tim,21,75,10 Jones St,END' & @crlf & _ 'rob,32,90,21 Smith St,END' & @crlf & _ 'will,65,72,5 Davis St,END' & @CRLF $string = stringreplace($string,',',' ') Local $array = StringRegExp($string, "(.*?) END", 3) For $i = 0 To UBound($array) - 1 $sResult &= "$array[" & $i & "] = " & $array[$i] & @LF Next ConsoleWrite($sResult & @LF) kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
jdelaney Posted October 29, 2012 Posted October 29, 2012 another way Local $sResult Local $string = 'name,age,weight,address,END' & @crlf & _ 'tim,21,75,10 Jones St,END' & @crlf & _ 'rob,32,90,21 Smith St,END' & @crlf & _ 'will,65,72,5 Davis St,END' & @CRLF $array = StringRegExp ($string, "(.*),",3) Dim $FinalArray[1] $iCounter = 0 For $i = 0 To UBound ( $array ) - 1 $array2 = StringRegExp ( $array[$i], "([wds]+),?",3) ReDim $FinalArray[$iCounter+1] $FinalArray[$iCounter] = $array2 $iCounter+=1 Next For $i = 0 To UBound ( $FinalArray ) -1 _ArrayDisplay ($FinalArray[$i]) Next IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
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