SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Reg Exp help

  1. #1
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    monkey - the rest is history

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,087
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Change it like so:

    Code:
    <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

    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.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Change it like so:

    Code:
    <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

    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
    monkey - the rest is history

  4. #4
    SitePoint Addict bronze trophy WolfShade's Avatar
    Join Date
    Mar 2014
    Location
    St. Louis, MO, USA
    Posts
    286
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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.

    ^_^

  5. #5
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WolfShade View Post
    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.

    ^_^
    <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!
    monkey - the rest is history

  6. #6
    SitePoint Addict bronze trophy WolfShade's Avatar
    Join Date
    Mar 2014
    Location
    St. Louis, MO, USA
    Posts
    286
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    <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.

    ^_^


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •