SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: help with regular expression

  1. #1
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with regular expression

    Hi people

    I need help with a simple regular expression.
    I am given a PHP buffer. I need to know if the following phrase appears in it:
    INSERT INTO <table name>

    where <table name> is a name of a sql table. It can contain characters (upper and lower) and underscores.
    Here are possible values:
    INSERT INTO mytable
    INSERT INTO another_table

    right after the name of the table needs to be a whitespace or newline.

    can anyone help me with that?

    regards
    Jason

  2. #2
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    preg_match_all('~INSERT\s+INTO\s+[a-z_]+\s~i'$buffer$m); 
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  3. #3
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kleineme View Post
    PHP Code:
    preg_match_all('~INSERT\s+INTO\s+[a-z_]+\s~i'$buffer$m); 
    You did say upper aswell?

    PHP Code:
    preg_match_all('~INSERT\s+INTO\s+[a-zA-Z_]+\s~i'$buffer$m); 
    Correct me if am wrong on that?

  4. #4
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,915
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by computerbarry View Post
    Correct me if am wrong on that?
    It's not wrong, it's just belt and braces. Check out the "i" modifier at the end of the expression.

  5. #5
    SitePoint Member
    Join Date
    May 2007
    Location
    Florida
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try the character class [[:space:]] in your regex. It is used for any white space.

  6. #6
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by onequickmemory
    Try the character class [[:space:]] in your regex. It is used for any white space.
    What is the benfit of using [:space:] instead of \s, which is also used for any white space? By the way, does anyone know why there is no explanation of these predefined character classes in the PHP-manual, except for an user note, although they use it in one of their examples?
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  7. #7
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,915
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kleineme View Post
    What is the benfit of using [:space:] instead of \s
    [:space:] is a POSIX character class, not PCRE. Don't try to use it with the preg functions.

  8. #8
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kleineme View Post
    What is the benfit of using [:space:] instead of \s, which is also used for any white space? By the way, does anyone know why there is no explanation of these predefined character classes in the PHP-manual, except for an user note, although they use it in one of their examples?
    who knows.. but check this out: http://www.htmlite.com/php034.php
    Last edited by voodoomagic; May 4, 2007 at 07:51. Reason: misread previous post

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kleineme View Post
    By the way, does anyone know why there is no explanation of these predefined character classes in the PHP-manual
    Because they forgot it?

    Anyways, here's the complete list (from http://lxr.php.net/source/php-src/ex...ompile.c#132):

    "alpha", "lower", "upper",
    "alnum", "ascii", "blank", "cntrl", "digit", "graph",
    "print", "punct", "space", "word", "xdigit"

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
  •