regex always gives me trouble, and I'm trying to get my mind better wrapped around it.

I am trying to figure out how to find a peice of text sandwiched between 2 partially known ends. The string looks like this:

33###text_to_find|||42

the numbers at the beginning and end are variable, and they can be any length. eg:

3###text_to_find|||42

32###text_to_find|||425
etc.

I thought this would work, but obviously it doesn't:

$string = "32###text_to_find|||425";
preg_match("/([0-9]+#{3})(|{3}[0-9]+)$/", $string, $matches);
echo($eh[0]);

Also, what is the point of the / before and after the regex expression. I've seen it done both ways, but I get an error if I try it without them.

TIA

    This should do it for you:

    <?PHP
    $string = "32###text_to_find|||425";
    preg_match("/^\\d*#{3}(.*?)\\|{3}\\d*$/", $string, $matches);
    echo($matches[1]); 
    ?>

    Also, the / at the beginning and end are the delimiters for the regular expression. Since PHP 4.0.4, you can use Perl style delimiters, such as (), {}, [], and <>. I personally use /, as that is the most standard way to do it.

    And the reason what you had didn't work is because you weren't using anything to match the text between the two known sections.

      Thanks for the help. I'll have to pull out my regex cheatsheet before I fully understand what you wrote.

      Thanks again.

        It'll match like this:

        ^ matches the begining of the string
        \d matches any number of digits
        #{3} matches 3 #'s
        (.
        ?) matches and remembers any number of any character ungreadily
        |{3} matches 3 |'s
        \d* matches any number of digits, again
        $ matches the end of the string

          Write a Reply...