SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    staten island
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help me out with a simple regex

    I am trying to write a simple regex to be used with preg_match

    Basicaly trying to match a list deliminated by new lines.
    The list has to have at least two items and an optional first item "none". The input is trimed so dont have to worry about leading or trailing newlines

    So here are a few examples of valid lists:

    none\nitem2\nitem2 , item1\nitem2 , item1\nitem2\nitem3 ...

    A few examples of invalid lists:

    item1 , none , none\nitem1

    At first i came up with this:
    /^(none\n)?[^\n]+\n[^\n]+/
    But it does not work because of greediness
    http://www.zaslavskiy.net
    felix[at]bebinary.com

  2. #2
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if( substr ($list 5) == "none\n" )
    $testme substr strstr ($list"\n") , ); }

    else
    $testme $list; }

    if ( 
    preg_match ("/[^\\n]+\\n[^\\n]+/" $testme))
    { echo 
    "match"; }

    else
    { echo 
    "not a match"; } 

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    staten island
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so is it impossible to make it in one shot ?
    http://www.zaslavskiy.net
    felix[at]bebinary.com

  4. #4
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's possible but i'm not that good with back referencing yet. it'd be a pretty advanced regex


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
  •