[Help]Regular Expression

keval007

Junior Member
Joined
Jun 12, 2012
Messages
144
Reaction score
26
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.
 
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.
 
Try this one:
Group: [0-9a-zA-Z-]*

It will select only the part you want to select.

Thanks
 
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.


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.


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]


And the pattern
$pattern = "%Group: [0-9a-zA-Z-]*%";
posted by blackhat777 also works
 
Code:
GROUP: (.*?),

This assumes you won't have a comma in the target string you want to extract.
 
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]

Ah right. I forgot that preg_match_all puts them into an array.
 
Back
Top