Jump to content

Return only numbers in a string


Recommended Posts

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

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 by Leighwyn
Link to comment
Share on other sites

@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

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

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...