Help - Search - Member List - Calendar
Full Version: Reg Exp Help
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Trina Espinoza
Var $machine_status contains this block of data:

machine_status = "Pinging 129.111.3.79 with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 192.111.3.79:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms"

I am trying to do a regular expression to see if the block contains the
string "Request timed out." from the block. My regular expression is below,
but it fails to find the string "Request timed out." What am I doing wrong?

if ($machine_status =~ /^Request timed out.$/m) {

Any help appreciated,

-T

John W. Krahn
Trina Espinoza wrote:
QUOTE
Var $machine_status contains this block of data:

machine_status = "Pinging 129.111.3.79 with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 192.111.3.79:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum =  0ms, Average =  0ms"

I am trying to do a regular expression to see if the block contains the
string  "Request timed out." from the block. My regular expression is
below, but it fails to find the  string "Request timed out." What am I
doing wrong?

if ($machine_status =~ /^Request timed out.$/m) {

It looks like there are spaces at the end of those lines. Also the .
character is special in regular expressions so you have to escape it.

if ($machine_status =~ /^Request timed out. *$/m) {



John
--
use Perl;
program
fulfillment

Gretar M Hreggvidsson
Hi

If "Request timed out" occurs in the output from PING command you can be
quite sure that the request timed out. :-)
Therefore it's be bit to much hazzle to check for embedded newlines
(using the m modifier), one could just say:

if ($machine_status =~ /Request timed out./) {

Remember to escape the period (.), it has a speciel meaning in regex
(can be any charachter more or less)

Maybe you should also consider using Net::Ping instead of using a system
call to the PING command! At least I hope you are checking the return
code from the system call. ;-)

Gretar Mar

Trina Espinoza wrote:
QUOTE
Var $machine_status contains this block of data:

machine_status = "Pinging 129.111.3.79 with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 192.111.3.79:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum =  0ms, Average =  0ms"

I am trying to do a regular expression to see if the block contains the
string  "Request timed out." from the block. My regular expression is
below, but it fails to find the  string "Request timed out." What am I
doing wrong?

if ($machine_status =~ /^Request timed out.$/m) {

Any help appreciated,

-T



Jeff 'japhy' Pinyan
On Jun 28, [Email Removed] said:

QUOTE
for (;<FH>;) {
if (/^-{5,}(w+)/ig) {
print $_;
}
$lc++;
}

I want to print everything from BEGIN TO END and right now all I am
printing is the begin and end lines.

If that's what you want, you should use two regexes with the .. operator:

while (<FH>) { # this is better-looking than 'for (;<FH>;)'
if (/^-----BEGIN/ .. /^-----END/) {
print;
}
}

The flip-flop operator (..) is *false* UNTIL the left expression evaluates
to true. THEN it STAYS *true* until the right expression evaluates to
true.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart

Jeff 'japhy' Pinyan
On Jun 28, [Email Removed] said:

QUOTE
but here is another piece of code I tried and this worked as well
considering the attachment:

comments on the below block?
[snip]
if (m/begin pgp public key block/ig) {
$lc=1;
}
if ( $lc==1){
print $_;
}
if (m/end pgp public key block/ig) {
$lc=0;
}

What comment do you want? This is 100% identical to using a flip-flop,
except that you have an explicit variable holding the truth value. It
starts as false. If the first regex matches, then $lc is true. If $lc is
true, print the current line. If the second regex matches, then $lc is
false.

Identical. Use a flip-flop. It's easier.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart

Jay Savage
On 6/28/05, [Email Removed] <[Email Removed]> wrote:
QUOTE
Can anyone help me with this match:

[snip]

QUOTE
for (;<FH>;)
{
if (/^-{5,}(w+)/ig) {
print $_;

}
$lc++;
}

[snip]

This says "if the line matches five dashes and at least one word
character (which you are capturing for some reason), then print the
line."

What you want is the exact opposite:

while (<FH>) {
print $_ unless /^-{5,}/ ;
}

And use while instead of the awkward "for (;<>;)" construction; this
is what it's there for.

-- jay
--------------------
daggerquill [at] gmail [dot] com
http://www.tuaw.com
http://www.dpguru.com
http://www.engatiki.org

Jay Savage
On 6/28/05, [Email Removed] <[Email Removed]> wrote:
QUOTE
Actually,

you are incorrect... The +>> mean read and write in append mode.  Please
see the follow up email with the attachment I sent.

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams


While you are correct, please don't do this. It's a great way to get
yourself into trouble and really only serves to obfuscate your code
and ensure that filehandles remain open when they aren't being used,
eating up kernel resources.

When you need to read from a file, open it, read from it, and close
it. then when you're ready to write to it, open it, write to it, and
close it.

If you really insist on obsfucation (using "for (;<>;)" and using your
own variable when $. or $.-1 would suffice indicate you might) make
sure you use appropriate select() calls to make sure you're not
getting premature EOF, and depending on your OS, also when switching
from reading to writing, and vice versa.

But really, the perl philosphy is to keep the simple things simple.
And reading a file, printing a few lines you want, and closing the
file is about as simple as it gets. in fact, it's a one-liner:

perl -lne 'print unless /^-{5}/' your_file

-- jay
--------------------
daggerquill [at] gmail [dot] com
http://www.tuaw.com
http://www.dpguru.com
http://www.engatiki.org

Jay Savage
On 6/28/05, Jeff 'japhy' Pinyan <[Email Removed]> wrote:
QUOTE
On Jun 28, [Email Removed] said:

but here is another piece of code I tried and this worked as well
considering the attachment:

comments on the below block?
[snip]
if (m/begin pgp public key block/ig) {
$lc=1;
}
if ( $lc==1){
print $_;
}
if (m/end pgp public key block/ig) {
$lc=0;
}

What comment do you want?  This is 100% identical to using a flip-flop,
except that you have an explicit variable holding the truth value.  It
starts as false.  If the first regex matches, then $lc is true.  If $lc is
true, print the current line.  If the second regex matches, then $lc is
false.

Identical.  Use a flip-flop.  It's easier.

--

Not quite. In this code, $lc is not set to zero until after the test
for $lc == 1, so the "-----END.+" line will be printed. A flip-flop on
the other hand--which you have now recommended at least twice--does
exactly what he's looking for.

-- jay
--------------------
daggerquill [at] gmail [dot] com
http://www.tuaw.com
http://www.dpguru.com
http://www.engatiki.org

Jay Savage
On 6/28/05, Jay Savage <[Email Removed]> wrote:
QUOTE
On 6/28/05, Jeff 'japhy' Pinyan <[Email Removed]> wrote:
On Jun 28, [Email Removed] said:

but here is another piece of code I tried and this worked as well
considering the attachment:

comments on the below block?
[snip]
if (m/begin pgp public key block/ig) {
$lc=1;
}
if ( $lc==1){
print $_;
}
if (m/end pgp public key block/ig) {
$lc=0;
}

What comment do you want?  This is 100% identical to using a flip-flop,
except that you have an explicit variable holding the truth value.  It
starts as false.  If the first regex matches, then $lc is true.  If $lc is
true, print the current line.  If the second regex matches, then $lc is
false.

Identical.  Use a flip-flop.  It's easier.

--

Not quite. In this code, $lc is not set to zero until after the test
for $lc == 1, so the "-----END.+" line will be printed. A flip-flop on
the other hand--which you have now recommended at least twice--does
exactly what he's looking for.

-- jay

I'm sorry, I read too fast. I thought in the original code the "begin"
was supressed. You are absolutely correct. The consecutive ifs yeild
exactly the same result as the flipflop. I hit send too hastily.

-- jay
--------------------
daggerquill [at] gmail [dot] com
http://www.tuaw.com
http://www.dpguru.com
http://www.engatiki.org


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.