Thread: Preg_match help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Preg_match help


    OK I'll be the first to admit I cant do a regex for the life of me, Ive been meaning to study it every time I can but I start and get distracted. So all that aside could someone help me out ???
    I'm trying to see if a string exists within a string but there might one character change. here is the string _(1) but the number may vary.

    Anyone feel like earning some good karma and alot of appreciation by giving me the answer ???
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    In a regex, you match can an unknown character from a specific set of characters using brackets. The brackets contain the possible characters. For example: [0123456789] will match any one digit.

    (However, there is actually a shortcut; [0-9] matches the range of characters 0 through 9, which is the same thing.)

    Parenthesis have special meaning in a regex, so you have to escape them.

    So, the regex: _\([0-9]\) will match the example you gave.

    However, this will only work if the number between the parenthesis is only one digit long. If you want to match numbers that are numerically greater than 9, then you must add a + sign after the [0-9]. The plus sign indicates that the proceeding component of the regex must exist one or more times.

    Comments on this post

    • MeccaOz agrees
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    10
    Rep Power
    0
    preg_match('%([a-z A-Z]+_\([0-9]+\))%s',$data,$matches); should solve this reg-ex problem and $matches[0] will give you the required value.
    Last edited by klipangel; May 6th, 2013 at 06:51 AM. Reason: to make answer more elaborate
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,968
    Rep Power
    374
    Originally Posted by klipangel
    preg_match('%([a-z A-Z]+_\([0-9]+\))%s',$data,$matches); should solve this reg-ex problem and $matches[0] will give you the required value.
    actually it wont. OP wants a number only and i think your regex matches numbers & alphabets
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    I guess klipangel only knows the characters a-zA-Z and cannot imagine that one might need more than that. Yes, there are in fact languages with "strange" characters like , , , , , , ... Some even have a completely different alphabet! Unbelieveable, isn't it?
    The 6 worst sins of security How to (properly) access a MySQL database with PHP

    Why cant I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo