Re: Expression problem
- From: Christian Winter <thepoet_nospam@xxxxxxxx>
- Date: Mon, 27 Nov 2006 22:57:00 +0100
K.J. 44 wrote:
I have the two following regular expressions. I am not very good at
writing these yet. I am parsing some logs looking for some key words,
then taking the text after them.
if ($details[$i] =~ /\bworkstation\b\bname:\b\s\b[0-9A-Za-z_\-]+\b/i) {
($nothing, $hostName[$i]) = split(/:/, $&);
}
if ($details[$i] =~ /\buser\b\bname:\b\w+/i) {
print("The username is: $&");
($nothing, $username[$i]) = split(/:/, $&);
}
The $details array is read in from a text file and this works fine.
What I want to do is search the $details text for certain key words,
then take the text right after. The first if statement
Find the word Workstation followed by a space followed by name:
followed by a space followed by a string of characters including word
characters and hyphens. if the match is found, take only the text
after the : as the workstation name.
The second part is along the same lines for username.
Find the word user followed a space followed by the word name: followed
by a space followed by a string of word characters. Split at the : as
the username found.
These do not seem to be finding matches when I can see them in the log
file. Where am I messing up?
In that \b is a _zero width_ assertion, so the part
workstation\b\bname
can never match, as to match there would need to be a non-word
character between the "\b"s. In your case, they are completely
redundant, as workstation\sname implies the word boundary and
is virtually the same as workstation\b\s\bname.
In fact, you can get rid of (at least) all but the first \b
in your expressions.
If you use capturing parentheses, you can also avoid the use
of $&, which might slow you script down, and get rid of the split()
which fetches the whitespace.
So the first piece of code could be cut down to
if( $details[$i] =~ /\bworkstation\sname:\s([0-9A-Za-z_\-]+)/i )
{
$hostName[$i] = $1;
}
There's still room for improvement, like e.g. removing A-Z from
the character class, as you are giving the /i modifier anyway,
so both upper and lowercase characters will be matched.
HTH
-Chris
.
- Follow-Ups:
- Re: Expression problem
- From: Tad McClellan
- Re: Expression problem
- References:
- Expression problem
- From: K.J. 44
- Expression problem
- Prev by Date: Re: Expression problem
- Next by Date: Re: Expression problem
- Previous by thread: Re: Expression problem
- Next by thread: Re: Expression problem
- Index(es):
Relevant Pages
|
|