Eagle117 Posted July 17, 2007 Share Posted July 17, 2007 I am reading a line from a Word document and I have it narrowed down to the field I want to read but it contains an ASCII character, 4-6 digits, a space, and then some letters. I want to only return the numbers that are all together in this string. Is there an easy function to only return the numbers in a string or would I have to write something that would look digit by digit to see when the numbers begin and go until the numbers end. An example of my string is this: 201669 JW All I want is 201669 returned. Is there a function I'm missing to do this easily? Link to comment Share on other sites More sharing options...
Leighwyn Posted July 17, 2007 Share Posted July 17, 2007 (edited) I am reading a line from a Word document and I have it narrowed down to the field I want to read but it contains an ASCII character, 4-6 digits, a space, and then some letters. I want to only return the numbers that are all together in this string. Is there an easy function to only return the numbers in a string or would I have to write something that would look digit by digit to see when the numbers begin and go until the numbers end. An example of my string is this: 201669 JW All I want is 201669 returned. Is there a function I'm missing to do this easily? If there really is only one character before the numbers, you can use: $str = "A201669 JW" $new = StringSplit($str," ") $final = StringTrimLeft($new[1],1) Edited July 17, 2007 by Leighwyn Link to comment Share on other sites More sharing options...
Siao Posted July 17, 2007 Share Posted July 17, 2007 (edited) StringRegExp($str, '\d+', 1) Edited July 17, 2007 by Siao "be smart, drink your wine" Link to comment Share on other sites More sharing options...
Eagle117 Posted July 17, 2007 Author Share Posted July 17, 2007 @Leighwyn I like your method and most of the time it would work for me but sometimes there isn't a space between the last digit and the letters so I need to make sure it is only numbers that I get. If people would type correctly then I wouldn't have to worry about this but oh well. @Siao Yours looks to be perfect but when I try it in my code, all I get is a blank space. I tried combining the two methods here so I trimmed off the first left character to get rid of the strange character and then only search what was left but I still get a blank instead of the numbers. Link to comment Share on other sites More sharing options...
Siao Posted July 17, 2007 Share Posted July 17, 2007 (edited) That StringRegExp returns an array if the pattern found.$str = '201669 JW'$ret = StringRegExp($str, '\d+', 1)if @error thenMsgBox(0,'','No match')elseMsgBox(0,'',$ret[0])endif Edited July 17, 2007 by Siao "be smart, drink your wine" Link to comment Share on other sites More sharing options...
Eagle117 Posted July 17, 2007 Author Share Posted July 17, 2007 That StringRegExp returns an array if the pattern found. $str = '201669 JW' $ret = StringRegExp($str, '\d+', 1) if @error then MsgBox(0,'','No match') else MsgBox(0,'',$ret[0]) endif Got it. I didn't know about the array part. Works beautifully. Link to comment Share on other sites More sharing options...
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