1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[Help]Regular Expression

Discussion in 'PHP & Perl' started by keval007, Jun 30, 2012.

  1. keval007

    keval007 Junior Member

    Joined:
    Jun 12, 2012
    Messages:
    145
    Likes Received:
    26
    Occupation:
    Web Scraper & PHP Developer
    Hi, PHP Experts


    I have text file which has lots of text as well as HTML data. There are some line as shown below.


    Code:
    06/30/12 00:40:30.73 Sending Email - Group: 4FD7917824F1B-EMAIL, 0, Message: 

    There are lots of lines like above in text file. I want to extract following data from the entire text file.


    Code:
    Group: 4FD7917824F1B-EMAIL

    I know this can be done by regular expression but I don't know how exactly done it. Some one can please provide some regular expression for above or some explanation.
     
  2. LongBanana

    LongBanana Regular Member

    Joined:
    Oct 23, 2009
    Messages:
    411
    Likes Received:
    247
    Location:
    Chicago, IL
    I believe this was the way to do it in PHP.
    Code:
    $myfile = file_get_contents('location of txt file');
    $pattern = '%([^,]Group.*)%';
    if (preg_match_all($pattern, $myfile, $matches, PREG_SET_ORDER))
    {
        foreach ($matches as $match)
        {
             echo $match;
        }
    }
    
    
    That should do the trick. Let me know what comes out of it.
     
    • Thanks Thanks x 1
  3. blackhat777

    blackhat777 Elite Member

    Joined:
    Jun 25, 2011
    Messages:
    1,779
    Likes Received:
    653
    Try this one:
    Group: [0-9a-zA-Z-]*

    It will select only the part you want to select.

    Thanks
     
    • Thanks Thanks x 1
  4. keval007

    keval007 Junior Member

    Joined:
    Jun 12, 2012
    Messages:
    145
    Likes Received:
    26
    Occupation:
    Web Scraper & PHP Developer


    Your script give output as "Array Array Array". I have done little modification in it and it works.

    Code:
    $myfile = file_get_contents('EmailLog.txt');
    $pattern = "%[^,](Group.*)-EMAIL%";
    
    if (preg_match_all($pattern, $myfile, $matches, PREG_SET_ORDER))
    {
        foreach ($matches as $match)
        {
             echo $match[0]."<br>";
        }
    }
    
    Output:
    [COLOR=#000000][FONT=Times New Roman]Group: 4FD7917824F1B-EMAIL[/FONT][/COLOR]
    [COLOR=#000000][FONT=Times New Roman]Group: 4FD7917824F1B-EMAIL[/FONT][/COLOR]
    [COLOR=#000000][FONT=Times New Roman]Group: 4FD7917824F1B-EMAIL[/FONT][/COLOR]
    [COLOR=#000000][FONT=Times New Roman]Group: 4FE49D61C1446-EMAIL[/FONT][/COLOR]
    [COLOR=#000000][FONT=Times New Roman]Group: 4FE49F94DE946-EMAIL[/FONT][/COLOR]
    [COLOR=#000000][FONT=Times New Roman]Group: 4FD3E154C80E3-EMAIL[/FONT][/COLOR]
    
    

     
  5. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    Code:
    GROUP: (.*?),
    This assumes you won't have a comma in the target string you want to extract.
     
  6. LongBanana

    LongBanana Regular Member

    Joined:
    Oct 23, 2009
    Messages:
    411
    Likes Received:
    247
    Location:
    Chicago, IL
    Ah right. I forgot that preg_match_all puts them into an array.