Re: Regex help!
| From: | Jeff P | Date: | Fri, 05 Jan 2001 01:28:36 +0000 |
| Subject: | Re: Regex help! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-32787@lists.php.net to get a copy of this message | ||
> Basically, I have a whopping great log file that I would like to parse. Each
> line of the log file consists of one of 70 or so sentences. EVERY sentence
> takes one of the following three forms:
>
> [keyword1]phrase1[keyword2]phrase2
> [keyword1]phrase1[keyword2]
> [keyword1]phrase1
look at ereg(); although efficiency experts may
want to chime in. how often will you be running
this? (can you change "phrase1" or "phrase2" in
you log format?)
this not-entirely-elegant snippet should work:
if ( ereg("(.*)phrase1(.*)phrase2", $input, $regs) ) {
/*
then we matched case 1; $regs[1] will
contain phrase 1; $regs[2] -> phrase2
".*" -V the "." means any character;
the "*" means zero or more times.
i am assuming that php does greedy
matches, in that the ".*" will suck
up as much text as possible.
if you need to force it to the beginning of
the line, use a "^" at the very beginning of the
regexp; a "$" will do the same for end-of-line
when placed at the end of the regexp.
*/
} elseif ( ereg("(.*)phrase1(.*)", $input, $regs) ) {
/*
then we matched case 2 or 3; $regs[1] will
contain phrase 1.
*/
}
best,
jeff