Snarg Posted March 12, 2006 Posted March 12, 2006 Ok, I have done a good deal of reading up on these things and now my head hurts. I am looking to read in a large ammount of text and filter it for an 18 digit number. I *think* I know how to formulate my RegEx however, could someone take a look at it for me? This should search for an 18 digit number, correct? \b[0-9]{1,18}\b If that is correct, what would be the proper way to include it in the function call? A little reading goes a long way. Post count means nothing.
greenmachine Posted March 12, 2006 Posted March 12, 2006 (edited) That didn't seem to work for me. However, this pattern did. $stringasdf = "this is a long string of random stuff and an 18 digit number with 4 or 5 letters on either side asdf123456789012345678ffdsa and stuff" $result = StringRegExp ($stringasdf, "[:digit:]{18}") MsgBox (0, "", $result) However, as of now it only returns 1 or 0 depending on if it matched the pattern. When I set the flag to 1 or 3 (which should return an array), it didn't return an array, and instead returned a blank string (but @extended was still 1, which means that it worked). The @error was also 0.. so I don't know about that one. Edited March 12, 2006 by greenmachine
greenmachine Posted March 12, 2006 Posted March 12, 2006 Double-posting only because I have a new breakthrough. I solved my issue. #include <array.au3> $stringasdf = "this is a long string of random stuff and an 18 digit number with 4 or 5 letters on either side asdf123456789012345678ffdsa and stuff" $result = StringRegExp ($stringasdf, "(\d){18}", 3) If @extended = 1 Then _ArrayDisplay ($result, "res") Else MsgBox (0, "", @error & @CRLF & @extended) EndIf Note: this matches 18 or more digits, and returns an array of the first 18 digits. I couldn't figure out how to limit it to 18 digits (yet). The array in this case will be the following: [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 6 [6] = 7 [7] = 8 [8] = 9 [9] = 0 [10] = 1 [11] = 2 [12] = 3 [13] = 4 [14] = 5 [15] = 6 [16] = 7 [17] = 8
neogia Posted March 12, 2006 Posted March 12, 2006 Good work, greenmachine, If you just modify it so that the capture "()" includes the "{18}" specification, it will find your number of 18 digits intact, and it will find all instances of 18 digits. $result = StringRegExp ($stringasdf, "([0-9]{18})", 3) [u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia
greenmachine Posted March 12, 2006 Posted March 12, 2006 Well in that case you can modify it to use the class like I had in the first place: $result = StringRegExp ($stringasdf, "([:digit:]{18})", 3) Either way works fine. Turns out I had been trying too many things at once with the group pattern, which was why I didn't have the {18} inside the group. I figured it should have worked inside.... Thanks for pointing that out.
neogia Posted March 12, 2006 Posted March 12, 2006 Ah, as I was testing to find the problem, I changed "\d" to "[0-9]" in one case and forgot to change it back. Sorry, I didn't mean it as a correction.. @others: "[0-9]", "[:digit:]", and "\d" are interchangeable Thanks greenmachine, nice work again. [u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia
Snarg Posted March 12, 2006 Author Posted March 12, 2006 Once again, thank you gentlemen. And again, I find myself so close and yet, so far away. OK, I can strip the number I need now with no problem. Now I am having trouble actualy *using* the number. I can not seem to assign the number to a variable. This is the code I am using: $body = _IEBodyReadHTML ( $o_IE ) $result = StringRegExp ($body, "([0-9]{18})", 1) If @extended = 1 Then _ArrayDisplay ($result, "res") ;MsgBox (0, "", $result) Else MsgBox (0, "", @error & @CRLF & @extended) EndIf I would like to be able to assign the results of _ArrayDisplay to a variable. When I do that (see the commented out MsgBox line) I get a blank display. I have tried alterting the _ArrayDisplay function to no avail. Below is the _ArrayDisplay function: Func _ArrayDisplay(Const ByRef $avArray, $sTitle) Local $iCounter = 0, $sMsg = "" If (Not IsArray($avArray)) Then SetError(1) Return 0 EndIf For $iCounter = 0 To UBound($avArray) - 1 $sMsg = $sMsg & StringStripCR($avArray[$iCounter]) Next MsgBox(4096, $sTitle, $sMsg) SetError(0) Return 1 EndFunc I have messed with this line to no avail: $sMsg = $sMsg & StringStripCR($avArray[$iCounter]) As usual, any help in this matter would be greatly appreciated. Thank you. A little reading goes a long way. Post count means nothing.
greenmachine Posted March 12, 2006 Posted March 12, 2006 When the array was displayed, it should have showed (if you used my example): [0] - 123456789012345678. This means that the 18-digit string (number) is stored in the array at location 0. So, in order to access that location and save it to a separate variable, simply do this: #include <array.au3> $stringasdf = "this is a long string of random stuff and an 18 digit number with 4 or 5 letters on either side asdf123456789012345678ffdsa and stuff" $result = StringRegExp ($stringasdf, "([:digit:]{18})", 3) If @extended = 1 Then _ArrayDisplay ($result, "res") $NewVar = $result[0]; stores the numbers to $NewVar Else MsgBox (0, "", @error & @CRLF & @extended) EndIf If you have more than 1 match, you might want to leave it in array form. You can find out how many matches were made by doing this: MsgBox (0, "number of matches", UBound ($result)) ; Note that arrays are 0-based, so the highest element would be Ubound ($result) - 1 -> in my case, ubound is 1, so the highest is 0.
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