Reg Exp help

I am try to get a regular expression to extract the numbers from a string (logged on user). This is what i have which used to work:

<cfset usrPos = REFind(“[[:digit:]]*$”,REMOTE_USER,1,“TRUE”)>
<cfset usrNo = Mid(REMOTE_USER,usrPos.pos[1],usrPos.len[1])>

returning 1234 as usrNo from REMOTE_USER DOMAIN\1234

however, some users now have a letter on the end which stops the above working. So i need to return 1234 from DOMAIN\1234a

Any help appreiciated
cheers

Change it like so:


<cfset usrPos = REFind("[[:digit:]]*[[:alpha:]]?$",REMOTE_USER,1,"TRUE")>

Alternatively you can use [[:lower:]] instead of [[:alpha:]] if you want to match lower case only, or (you guessed it) [[:upper:]] for upper case :slight_smile:

The ? after [[:alpha:]] means: 0 or 1 letters must be here. If there is more than 1 letter it won’t match, but with one letter or no letter it will.

Thanks for the reply - almost what i need but not quite! I only want to return the numbers so domain\1234a should return 1234 without the ‘a’
cheers

Unless there is a strict requirement for checking just the end of the string, you could remove the ‘$’ at the end. That way, it will take numbers from anywhere in the string and return only the numbers. If you don’t use the ‘g’ global attribute, it will return only the first instance of numbers.

:slight_smile:

<cfset REMOTE_USER2=“domain\1234a”>

<cfset usrPos = REFind(“[[:digit:]]*[[:alpha:]]?”,REMOTE_USER2,1,“TRUE”)>
<cfset usrShoulder = Mid(REMOTE_USER2,usrPos.pos[1],usrPos.len[1])>

usrShoulder is d!

<cfset REMOTE_USER2=“domain\1234a”>

<cfset usrPos = REFind(“[[:digit:]]+”,REMOTE_USER2,1,“TRUE”)>
<cfset usrShoulder = Mid(REMOTE_USER2,usrPos.pos[1],usrPos.len[1])>

Sorry… didn’t scan all of it as well as I thought I had. If all you’re looking for is the number, don’t look for the letter(s) after.

:slight_smile: