Regular expression for parsing curly braces

Discussion in 'PHP' started by amit.patel, Oct 21, 2008.

  1. #1
    Hello all

    I want regular expression which parse the word containing the curly braces means

    in string there is word like {TEXT} i want to serach this text within this string , So i

    use
    preg_match("/\b".$word."\b/i", $string)
    Code (markup):
    it's working when i use

    without curly braces but when i use curly braces it's doesn't working

    So please help me

    Thanks and regards,
    ---Amit Patel
     
    amit.patel, Oct 21, 2008 IP
  2. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #2
    
    preg_match('~{(.*?)}~', $string)
    
    PHP:
    Should do it, though you may have to escape the curly braces.
     
    blueparukia, Oct 21, 2008 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    preg_match("#\{$word\}#i", $str);
    Code (markup):
    Curley braces denote an "N or more times" syntax, so yes, they need to be escaped when used.
    For instance the following will match 2-7 zeros.

    #0{2,7}#
    Code (markup):
    You can use preg_quote any time you're unsure about metacharacters.
     
    joebert, Oct 22, 2008 IP