#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    76
    Rep Power
    8

    Pattern matching


    hello

    is there any way to get array of all element coming under some specific pattern.

    ex i have a string. now i want array of all data coming under [[[ * ]]] such pattern

    where * is data under this pattern

    plz help how i can solve this isuee
    Last edited by ksandeep; March 19th, 2010 at 05:27 AM.
  2. #2
  3. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    you mean like preg_match_all()?
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    76
    Rep Power
    8
    Originally Posted by Northie
    you mean like preg_match_all()?
    yes something smiler to that but i am not able for creating regular expression for pattern [['[ desire data goes here]]]
  6. #4
  7. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    how do you intend to do it without a regular expression?
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    76
    Rep Power
    8
    Originally Posted by Northie
    how do you intend to do it without a regular expression?
    i tried but not able to generate regular expression pl help ho i can

    my patter come in [[[ ]]]


    three left & 3 right breckets
  10. #6
  11. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    there's a regex forum here on devshed - try in there
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  12. #7
  13. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    Moving to the RegEx forums.

    Comments on this post

    • ManiacDan agrees : Bah, that's what I came here to do.
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  14. #8
  15. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    Explain your problem more clearly. You want to match elements to a string? Elements; as in X/HTML elements? Do you mean the element type? Be more precise.
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    76
    Rep Power
    8
    Originally Posted by Winters
    Explain your problem more clearly. You want to match elements to a string? Elements; as in X/HTML elements? Do you mean the element type? Be more precise.
    i have a string & i want to find all accurate of sub-string coming under [[[ & ]]]

    mean 3 [ in left & 3 in right
  18. #10
  19. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    This?
    PHP Code:
      preg_match_all("/\[\[\[ & \]\]\]/", $originalString, $outputArray);
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    76
    Rep Power
    8
    its not solve my purpose
    i explain by example i have string like

    The page you requested cannot be found. The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.The page you requested cannot be found. The page [[[cart]]] you are looking for might have been removed, had its name changed, or is temporarily unavailable.The page you requested [[[about-us]]]cannot be found. The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.The page you [[[forget-password]]]requested cannot be found. The page you are looking [[[catalog]]]for might have been removed, had its name changed, or is temporarily unavailable.The page you requested cannot be found. The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

    now my output array should have values cart, about-us, forget-password, catalog
  22. #12
  23. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    back to php!

    I have found that with consistent delimiters that explode and loop is faster than regex - a lot faster

    eg

    PHP Code:
    $tmp explode("[[[",$input_string);

    for(
    $i=0;$i<count($tmp);$i++) {
        list(
    $a,b) = explode("]]]",$tmp[$i]);
        
    $matches[] = $a;

    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  24. #13
  25. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    As Northie says, if the delimiters are static and constant, then regex will most likely be a lot slower. However, you may want to refine the code a bit Northie, as it looks as though he is only looking for a single match and a loop may not be required.

    Either way, here is the regex.
    PHP Code:
      preg_match_all("/\[{3}([\w\d-_]+)\]{3}/", $originalString, $outputArray);
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  26. #14
  27. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    That last regex will fail on things like [[[home.page]]], try this:
    PHP Code:
    preg_match_all("/\[{3}([^\]]+)\]{3}/"$originalString$outputArray); 
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo