Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Senior Member raspberryh's Avatar
    Join Date
    Aug 2004
    Location
    Erie, PA
    Posts
    882
    Member #
    7208
    Liked
    1 times
    I am having so much trouble making this regular expression.

    I want to get everything between [list start] and [list end] (including the [list start] and [list end]).

    I've tried this:
    Code:
    (\[list start\])[.*](\[list end\])
    But I get this:
    Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '[' in E:\Domains\allstarfg.com\wwwroot\news.php on line 25
    I've tried this:
    Code:
    (\[list start\]).*(\[list end\])
    But I get this:
    Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '.' in E:\Domains\allstarfg.com\wwwroot\news.php on line 25
    I've tried this:
    Code:
    (\[list start\])[.]*(\[list end\])
    But I get this:
    Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '[' in E:\Domains\allstarfg.com\wwwroot\news.php on line 25
    I've tried this:
    Code:
    (\[list start\])([.]*)(\[list end\])
    But I get this:
    Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '(' in E:\Domains\allstarfg.com\wwwroot\news.php on line 25
    ...And let me also point out that this
    Code:
    (\[list start\])
    by itself gives no errors.

    Please help :dead:
    Thanks
    choosy developers choose gif!
    website | paintings | blog

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Remember to use double-escaping when quoting a regex in PHP - otherwise, it tries to escape the input string.

    try this:

    PHP Code:
    preg_match_all("\\[list start\\].*\\[list end\\]"$in$out); 

  4. #3
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Remember you need to check for case-insensitivity, multiple lines, and not-greedyness.
    PHP Code:
    preg_match_all("/\[list start\].*\[\/list start\]/siU"$string$matches); 
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  5. #4
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,656
    Member #
    1234
    Liked
    137 times
    Would you need to combine transio's and filburt's suggestions?
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  6. #5
    Senior Member raspberryh's Avatar
    Join Date
    Aug 2004
    Location
    Erie, PA
    Posts
    882
    Member #
    7208
    Liked
    1 times
    I don't understand what either of them have done by adding the extra slashes... I don't understand why they're needed. It seems like Transio is escaping the slash which I am supposed to be using to escape the [. And Filburt's forward slash, well that's just confusing. Why does that even need to be there?
    choosy developers choose gif!
    website | paintings | blog

  7. #6
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    The forward slash generally delimits a regular expression. In languages where regexps are first-class citizens, like Perl or Ruby, they aren't even contained in strings -- instead, they're contained in a pair of /s. Sometimes, that gets carried over into languages where they *aren't* first-class citizens, which is what's happening in filburt's regexp. Moreover, adding those in in filburt's regexp allows you to add the regexp modifiers siU to the end, which handle case insensitivity, `multi-lineness', and not-greedyness.

    As for transio's, example, when you do `\[' in a string, PHP goes `oh, look, [ might be a special string character, so we'll make sure to insert it literally', which is not what you want. What you want is to say `[ is a special regexp character, so make sure there's a \ before it so the regexp engine interprets it literally'. Thus, yes, you need to escape the `\' so that PHP inserts a literal `\' followed by a literal `[' so that the regexp engine sees a literal `['. Simple, eh? :-P

  8. #7
    Senior Member raspberryh's Avatar
    Join Date
    Aug 2004
    Location
    Erie, PA
    Posts
    882
    Member #
    7208
    Liked
    1 times
    Quote Originally Posted by Shadowfiend
    The forward slash generally delimits a regular expression. In languages where regexps are first-class citizens, like Perl or Ruby, they aren't even contained in strings -- instead, they're contained in a pair of /s. Sometimes, that gets carried over into languages where they *aren't* first-class citizens, which is what's happening in filburt's regexp. Moreover, adding those in in filburt's regexp allows you to add the regexp modifiers siU to the end, which handle case insensitivity, `multi-lineness', and not-greedyness.
    Hm, interesting... Thanks for the explanation!

    As for transio's, example, when you do `\[' in a string, PHP goes `oh, look, [ might be a special string character, so we'll make sure to insert it literally', which is not what you want. What you want is to say `[ is a special regexp character, so make sure there's a \ before it so the regexp engine interprets it literally'. Thus, yes, you need to escape the `\' so that PHP inserts a literal `\' followed by a literal `[' so that the regexp engine sees a literal `['. Simple, eh? :-P
    Aaaaaaaaaah, now it makes sense! Wow, thanks for explaining that!

    And thanks Filburt and Transio for your help. My regex works now
    choosy developers choose gif!
    website | paintings | blog


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:43 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com