Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    280
    Thanks
    25
    Thanked 9 Times in 9 Posts

    regular expression

    Ok, i've been trying to learn regular expressions.

    Code:
    ([\s|.]*<(p|P|(H|h)(?(3)[1-6]))+>(.*)</(p|P|(H|h)(?(3)[1-6)]))+>+[\s|.]*)
    is the first i have wrote from scratch, it finds header tgs and paragraph tags (with no atributes)

    Code:
    <h3>Commercial & Domestic</h3>
    it would pick up.

    Problem is

    Code:
    <h3>Commercial & 
    Domestic</h3>
    or

    Code:
    <h3>
    Commercial & Domestic
    </h3>
    It would not pick up

    anyone know of a way to help?

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Your regexp seems very ineffecient.. I made this up, and the flag "s" treats the string as a single line.

    PHP Code:
    <?php

    $string 
    = <<<HTML
    <h2>Header</h2>
    <p>Paragraph</p>
    HTML;

    preg_match_all('#<(?:h[1-6]|p)>(.*?)</(?:h[1-6]|p)>#is'$string$matches);

    print_r($matches);

  • #3
    Regular Coder
    Join Date
    Sep 2008
    Posts
    280
    Thanks
    25
    Thanked 9 Times in 9 Posts
    Right, could you explain to me is PHP regular expression different to other regular expression? for instance, i've downloaded a program called Expresso code project to test regular expressions. I copyied and paste what you put into there and tested and came up with no results. (apart from when i took away (# and #is, but then i was where i was before)

  • #4
    Regular Coder
    Join Date
    Sep 2008
    Posts
    280
    Thanks
    25
    Thanked 9 Times in 9 Posts
    Also, you used ?: when i want to match that as i want to know what data came from what tag.

  • #5
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Okay well if you want to match what tag it is simply remove the ?: like you said on the first one. Also I've never used Expresso Code Project, and I can't even manage to get your regular expression to work in PHP by simply adding decimeters like // (or ##, @@, etc). So it's possible that your Expresso Code Project tool is making incompatible regular expressions or you are. Develop them in PHP and test them in PHP. Unless you're using ereg? Seriously I would just scrap your last one and stick with the one I made although it's not perfect it's not as messy and inefficient as yours. Personally I don't recommend using a program to make regexp so if you are, I'd reconsider.
    Last edited by Iszak; 05-02-2009 at 11:57 AM.

  • #6
    Regular Coder
    Join Date
    Sep 2008
    Posts
    280
    Thanks
    25
    Thanked 9 Times in 9 Posts
    what i cant seem to get to work is the () (?($1)do this|or this)


    Is this right or have i ot it round my neck?

    if submatch $1 is set then do this.

    thats what i want.

  • #7
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    No that is wrong, if you want to use something you matched prior in the regular expression you use \n for single quotes or \\n for double quotes (where n is a number) e.g.
    Code:
    preg_match_all('#<(h[1-6]|p)>(.*?)</\1>#is', $string, $matches);
    So whatever they matched first must be used later and you can't do <h1>Header</p>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •