Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PERL Pattern Matching

Status
Not open for further replies.

pbatt

Programmer
Sep 21, 2005
15
US
Hello,

I'm searching through a generated file of HTML code and am trying to abstract a date. Basically, I begin by encapsulating a small portion of the file using 'until' knowing that there are 2 strings guarenteed to be there.

The date is of the format 2005-09-21 07:45:19.0

Can anyone help with a way to search for this date, then assign it to a variable? I tried using /-- ::./ but that doesn't match?

Thanks!

 
Code:
/(\d\d\d\d-\d\d-\d\d\s+\d\d:\d\d:\d\d\.\d)/
S1 will hold the matched string.


Trojan.
 
Ok, the string match makes clear sense, but what do you mean when you refer to S1?

Thank you!
 
Sorry, $1 (dollar 1)
It's the scalar that the resultant match will be stored in (due to the parentheses).


Trojan.
 
Does this make sense, I'm a little confused on how to assign the pattern match:

$_ = /(\d\d\d\d-\d\d-\d\d\s+\d\d:\d\d:\d\d\.\d)/;
print "$_\n";

Or should I add the /(\d\d.....)/ to an 'if' statement, and print $1?
 
Yep rhash, I meant $1.
pbatt,
Assuming your string is in $_ you just need to:
Code:
/(\d\d\d\d-\d\d-\d\d\s+\d\d:\d\d:\d\d\.\d)/;
print "[red]$1[/red]\n";

Trojan.
 
Better though would be to;
Code:
/(\d\d\d\d-\d\d-\d\d\s+\d\d:\d\d:\d\d\.\d)/
and print "$1\n";
So as to only print $1 if the match succeeds.


Trojan.
 
By default m// will search in $_, right?

When I use that, I get a "Use of uninitialized value in concatenation (.) or string at..."

Do you know what this means?
 
Ok, got it working. Still getting a "Use of uninitialized value in pattern match at line..." Had to add an extra -\d\d since there is a date and month field.

I'll ignore that error since I'm getting the result I want, is that bad?

Thank you very much for your help!
 
The "Use of uninitialized value in pattern match at line..." means that sometimes "$_" is undefined. If it's undefined then it's very empty indeed so I guess you'll get away with that!
Still, you might do well to find out why your $_ is sometimes undefined.


Trojan.
 
Ok, I'll check it out.

Thanks again for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top