Results 1 to 10 of 10

Thread: Regular expression question - any number of spaces

  1. #1
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default Regular expression question - any number of spaces

    Hi all, currently I am working with this pattern

    PHP Code:
    $pattern '/<!--' $tagName '\\s-->/'
    This matches <!--example --> but not <!-- example -->

    How should I adjust the regular expression so that it will match <!-- example --> as well as

    PHP Code:
    <!--   example    --> 

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    This was my first attempt. It works, but allows for unequal spaces on either side of tagname. 0 on one side and 1 on the other will match:

    Code:
    '/^(<!\-\-)\s?' . $tagname . '\s?(\-\->)$/'
    My second attempt will force the tagname to either be surrounded by one space or no spaces, but looks a bit sloppy. I think there is another way to write it, but I couldn't figure it out. Hopefully someone here knows how because I would like to know too.

    Code:
    '/^(<!\-\-)(' . $tagname . '| ' . $tagname . ' )(\-\->)$/'
    PHP Code:
    <?php

    $tagname 
    'tagname';
    $correct '<!-- tagname -->';
    $pattern '/^(<!\-\-)\s?' $tagname '\s?(\-\->)$/';
    //$pattern = '/^(<!\-\-)(' . $tagname . '| ' . $tagname . ' )(\-\->)$/';

    if (preg_match($pattern$correct))
        echo 
    'Matched';
    else
        echo 
    'Did not match';

    exit;

  3. The Following User Says Thank You to JasonDFR For This Useful Post:

    CrazyChop (03-22-2009)

  4. #3
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    This might seem cleaner:
    PHP Code:
    <?php
    $string 
    'Hello world <!-- example   --> this is a test';
    preg_match('/<!-- *(example) *-->/'$string$matches);
    print_r($matches);

  5. The Following User Says Thank You to techietim For This Useful Post:

    CrazyChop (03-23-2009)

  6. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by CrazyChop View Post
    How should I adjust the regular expression so that it will match <!-- example --> as well as

    PHP Code:
    <!--   example    --> 
    To add to my first reply, if you want to allow any number of spaces on either side of $tagname, you can use * in place of the ? in this regex:

    PHP Code:
    '/^(<!\-\-)\s?' $tagname '\s?(\-\->)$/'
    '/^(<!\-\-)\s*' 
    $tagname '\s*(\-\->)$/' 
    I think there is a clean way to make sure that the same number of spaces exists on either side of $tagname as well, but again, I haven't been able to figure it out.

  7. The Following User Says Thank You to JasonDFR For This Useful Post:

    CrazyChop (03-23-2009)

  8. #5
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    @Jason

    That's called back referencing, and you'd do it like this:
    PHP Code:
    <?php
    $string 
    'Hello world <!-- example --> this is a test';
    preg_match('/<!--( )*(example)\1-->/'$string$matches);
    print_r($matches);

  9. The Following User Says Thank You to techietim For This Useful Post:

    CrazyChop (03-23-2009)

  10. #6
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Smile

    Thanks Tim. I have been reading a bit about that too, but couldn't get one working just right.

    Run this code:

    PHP Code:
    <?php
    /* $string = 'Hello world <!-- example --> this is a test';
    preg_match('/<!--( )*(example)\1-->/', $string, $matches);
    print_r($matches); */

    $tagname 'tagname';
    $correct '<!--    tagname -->'// Different amount of spaces on each side. Still matches.
    $pattern '/<!--( )*(' $tagname ')\1-->/';
    //$pattern = '/<!-- *(' . $tagname . ') *-->/';
    //$pattern = '/^(<!\-\-)\s?' . $tagname . '\s?(\-\->)$/';
    //$pattern = '/^(<!\-\-)(' . $tagname . '| ' . $tagname . ' )(\-\->)$/';

    if (preg_match($pattern$correct))
        echo 
    'Matched';
    else
        echo 
    'Did not match';

    exit;
    Also, in what situation is it a good to add $matches and return the array? In the last post you made it results in 3 keys. The first two are empty and the third's value is 'example'.

    Thanks Tim. PS: Look at my directory copier class !!!

  11. The Following User Says Thank You to JasonDFR For This Useful Post:

    CrazyChop (03-23-2009)

  12. #7
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    This should do it:
    PHP Code:
    <?php 
    /* $string = 'Hello world <!-- example --> this is a test'; 
    preg_match('/<!--( )*(example)\1-->/', $string, $matches); 
    print_r($matches); */ 

    $tagname 'tagname'
    $correct '<!--    tagname -->'// Different amount of spaces on each side. Still matches. 
    $pattern '/<!--( *)(' $tagname ')\1-->/';
    //$pattern = '/<!--( )*(' . $tagname . ')\1-->/'; 
    //$pattern = '/<!-- *(' . $tagname . ') *-->/'; 
    //$pattern = '/^(<!\-\-)\s?' . $tagname . '\s?(\-\->)$/'; 
    //$pattern = '/^(<!\-\-)(' . $tagname . '| ' . $tagname . ' )(\-\->)$/'; 

    if (preg_match($pattern$correct)) 
        echo 
    'Matched'
    else 
        echo 
    'Did not match'

    exit;
    [0] is the full comment, so it would only be visible if you view the source of the page
    [1] contains the number of spaces on both sides
    [2] is the comment string

  13. The Following 2 Users Say Thank You to techietim For This Useful Post:

    CrazyChop (03-23-2009),JasonDFR (03-22-2009)

  14. #8
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Very nice. Now this regex is great. However, [1] is still empty?

    Hehe about the comment showing up in source only. Same thing happened to me when I was building the original regex. But naturally I overlooked that again.

    See ya.

  15. The Following User Says Thank You to JasonDFR For This Useful Post:

    CrazyChop (03-23-2009)

  16. #9
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    It's not actually empty, it contains the spaces that are before the comment name.

  17. #10
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Excellent. Thanks.

    Mystery spaces are here:

    PHP Code:
    $s str_replace(' ''s'$matches[1]);
    echo 
    $s

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
  •