SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Temecula, CA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regular expression pattern delimeter

    Every example I've looked at in this forum has used the '/' to indicate the beginning and ending of a pattern string, but it doesn't work for me. This works for me:
    PHP Code:
    $pattern '^<div class=\"refnamediv\"><h1>[A-Za-z0-9]*</h1>^'
    Is there something I don't know here? I mean, something that I don't know that I don't know. There's so much I don't know about regex, but this issue is... well, I just don't know!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,237
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)

    regex delimiters

    AFAIK you can use just about any character for a delimiter. I've seen a lot of examples that use the # character. I guess the idea is to use something that won't appear in the expression to help keep things clear. IMHO I wouldn't use the caret ^, as that has special meaning in regex.

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    header("content-type: text/plain");

    $str='<div class="refnamediv"><h1>a1</h1>';

    $pattern '/<div class="refnamediv"><h1>[a-z0-9]*<\/h1>/i';

    preg_match($pattern$str$matches);

    print_r($matches); 
    will output:
    Code:
    Array
    (
        [0] => <div class="refnamediv"><h1>a1</h1>
    )
    my mobile portal
    ghiris.ro

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The delimeter can be anything. But if it appears in your regular expression statement you must escape it with backslash. So the reason the / wasn't working with your string was because the </h1> existed. In the post above, it is escaped, so it would work.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Temecula, CA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Exactly the problem. I get it now. The first character is interpreted as "the pattern string will end when you see me again".

    Thanks to all of you.


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
  •