SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: preg_match help

  1. #1
    SitePoint Zealot ameerulislam's Avatar
    Join Date
    Jul 2011
    Posts
    145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    preg_match help

    I came across these two code snippets that I'm having hard time understanding it.. Not finding enough information about it online too..

    PHP Code:
    preg_match("/.*h.*e.*l.*l.*o.*/",$line
    I'm having problem understanding with this part >> "/.*h.*e.*l.*l.*o.*/"

    why * been used and is . simply means concatenation?

    another case

    PHP Code:
    preg_match('/(1111111|0000000)+/'$ConsecutiveZero
    '/(1111111|0000000)+/'

    what does | means here? is it OR I thought php or was ||
    and what does that + sign do at the end?

    Thanks in advance..

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,268
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    These are regular expressions. Based on your questions, it sounds like you need more than just a few sentences of summary, so I'll refer you instead to PHP's documentation on this subject.

    http://www.php.net/manual/en/book.pcre.php

    and

    http://www.php.net/manual/en/referen...ern.syntax.php
    "First make it work. Then make it better."

  3. #3
    SitePoint Zealot ameerulislam's Avatar
    Join Date
    Jul 2011
    Posts
    145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    These are regular expressions. Based on your questions, it sounds like you need more than just a few sentences of summary, so I'll refer you instead to PHP's documentation on this subject.

    http://www.php.net/manual/en/book.pcre.php



    and

    http://www.php.net/manual/en/referen...ern.syntax.php
    Thanks a lot.. I'm referring to those.

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This has always been my favorite site for regex. http://www.regular-expressions.info/
    - Robert

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Jeff is entirely correct in that if you're starting to dig into regex you should spend some time to learn fully what they mean. However, to answer your immediate inquiries...

    Quote Originally Posted by ameerulislam View Post
    I came across these two code snippets that I'm having hard time understanding it.. Not finding enough information about it online too..

    PHP Code:
    preg_match("/.*h.*e.*l.*l.*o.*/",$line
    I'm having problem understanding with this part >> "/.*h.*e.*l.*l.*o.*/"

    why * been used and is . simply means concatenation?
    * is used here as an indicator to say "0 or more".
    the . means "any character".
    so, this regular expression is read to be: Any number of characters, followed by an h, followed by any number of characters, followed by an e (etc, etc)

    another case

    PHP Code:
    preg_match('/(1111111|0000000)+/'$ConsecutiveZero
    '/(1111111|0000000)+/'

    what does | means here? is it OR I thought php or was ||
    and what does that + sign do at the end?
    || is OR in php. But regex syntax is not PHP. | means OR in PCRE.
    the +, again, is a numeric quantifier, meaning "1 or more".
    () are used to designate subpatterns.
    so this regular expression reads:
    "Either '111111' or '000000', followed by any number of instances of either '111111' or '000000'."
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •