Preg_Match within [ ]

Discussion in 'PHP' started by Silver89, Nov 27, 2009.

  1. #1
    I'm having trouble matching text and numbers within these types of square brakets []

    I'm trying the following but no luck...

    
    '~[(.*?)]~s'
    
    PHP:
     
    Silver89, Nov 27, 2009 IP
  2. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    '~\[(.*?)\]~s'

    just add slashes
     
    xenon2010, Nov 28, 2009 IP
    Silver89 likes this.
  3. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #3
    ah thanks that's great, it's always the smallest little problem in php
     
    Silver89, Nov 28, 2009 IP
  4. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    no problem..
    Rep me up :D
     
    xenon2010, Nov 28, 2009 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    Technically it's not a PHP problem, it's an issue with the Perl-compatible regular expression syntax the preg_ functions use.

    Yeah yeah I know I'm being a sort of grammer Nazi here, but there are lots of things that use Perl-compatible regular expressions. The quicker you learn that the syntax is its' own separate thing and not actually a "PHP thing", the more you'll be able to learn. :)
     
    joebert, Nov 28, 2009 IP
  6. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #6
    Yeh sure that's fine, but the regular expression was within a php function so it fits in this category as other people will understand what I'm trying to achieve with the php function in the first place.
     
    Silver89, Nov 28, 2009 IP
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    That function is just a wrapper for the Perl-compatible regular expression library. Your question doesn't even include syntax that isn't part of the regular expression. :)
     
    joebert, Nov 29, 2009 IP
  8. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    So you're saying:

    1) what he was asking was to do with Perl-compatible regular expressions, nothing to do with PHP
    2) he should have said 'it's always the smallest problems in regex' since whether or not he's using the regex itself in Perl or in PHP's preg_match, it all boils down to regex concerns

    ?
     
    szalinski, Nov 30, 2009 IP