Capel Posted November 17, 2009 Posted November 17, 2009 I want to parse some text using StringRegExp. Here is my text: SMITH,A H00005207 HT: 165 WT: 61. .1 235 cm Kg AGE/SEX: 27/F BSA: 1.68 LOC m2 R/B : W5TH : Here is my code: $chars = FileRead($hFile) $sPTnum = StringRegExp($chars,"H[0-9]{8}") ConsoleWrite("error:" & @error & @CRLF) ConsoleWrite("PTnum: " & $sPTnum & @CRLF) The problem is that the RegExp does not return a match. If I use a tool like Expresso to test the RegExp it works fine. What am I doing wrong?
Authenticity Posted November 17, 2009 Posted November 17, 2009 (edited) Read the function description in the help file. If the function succeeded and the third parameter is not 0 the return value is an array of matches where $sPTnum[0] is the first match or the first capturing parentheses. $sPTnum = StringRegExp($chars,"H[0-9]{8}", 1) ; 1 to return the match. If Not @error Then ConsoleWrite("PTnum: " & $sPTnum[0] & @CRLF) Edited November 17, 2009 by Authenticity
Capel Posted November 17, 2009 Author Posted November 17, 2009 Thanks. I had read the description and tried accessing the array, but I was still not getting a match. The problem was with my text. It is generated via OCR and there O's where there should have been 0's and thus no match.
Moderators SmOke_N Posted November 17, 2009 Moderators Posted November 17, 2009 (edited) #include <Array.au3> ; For Array display only Global $s_fread = FileRead("File") Global $a_data = StringRegExp($s_fread, "\w((?:o|O|\d){8})", 3) ; Fix Bad Char(s) For $i = 0 To UBound($a_data) - 1 $a_data[$i] = StringReplace($a_data[$i], "o", "0") Next _ArrayDisplay($a_data) Edited November 17, 2009 by SmOke_N Fixed Code Tags Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Capel Posted November 17, 2009 Author Posted November 17, 2009 #include <Array.au3> ; For Array display only Global $s_fread = FileRead("File") Global $a_data = StringRegExp($s_fread, "\w((?:o|O|\d){8})", 3) ; Fix Bad Char(s) For $i = 0 To UBound($a_data) - 1 $a_data[$i] = StringReplace($a_data[$i], "o", "0") Next _ArrayDisplay($a_data) Smoken, this is a great idea.
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