Sign in to follow this  
Followers 0
Eagle117

Return only numbers in a string

6 posts in this topic

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?

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by Leighwyn

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

StringRegExp($str, '\d+', 1)

Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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"

Share this post


Link to post
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.

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  
Followers 0