Jump to content
Sign in to follow this  

StringRegExp Question

Recommended Posts


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?

Share this post

Link to post
Share on other sites

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 by Authenticity

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

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


Edited 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.

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.