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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex issue

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Joined
Feb 6, 2002
Messages
1,851
Location
IL
Hi,

I have a string
$filename="File: C:\temp\perf_monitor_02280905.csv"

Trying to get the file path using regex:
$filename =~ m/File:\s+(\w:.*)/;

I'm getting the same string back.What is the catch here ?
Thanks

Long live king Moshiach !
 
how is it that you expect this:

Code:
$filename =~ m/File:\s+(\w:.*)/;

to change $filename in anyway? Plus using double-quotes means \t is interpolated as a tab and I think \p is a meta character too.

Code:
[blue]$filename[/blue]=[red]'[/red][purple]File:         C:\temp\perf_monitor_02280905.csv[/purple][red]'[/red][red];[/red]
[blue]$filename[/blue] =~ [red]m/[/red][purple]File:[purple][b]\s[/b][/purple]+([purple][b]\w[/b][/purple]:.*)[/purple][red]/[/red][red];[/red]
[blue]$foo[/blue] = [blue]$1[/blue][red];[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$foo[/blue][red];[/red]


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin,

the string is a result of a system command :
my $filename = `logman query perf_log|find\ "File\"`

So I do not have much control of it's output.
So how should I play the regex in this situation ?
Thanks

Long live king Moshiach !
 
maybe:

Code:
my $filename = `logman query perf_log|find\ "File\"`;
$filename =~ s/^File\s+//;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yes, the catch here, as KevinADC I think pointed out already, is that a regexp match on itself returns a truth value (it either matched, or it didn't). And it sets $1, $2, etc, if you used brackets in your pattern.
So
Code:
$filename =~ m/File:\s+(\w:.*)/;
will leave $filename untouched.
A substitution match ($string =~ s/pattern/replacement/), however, does alter the part left of the =~.

So that is why:
Code:
$filename =~ s/^File\s+//;
does work.
 
you can do this too:

Code:
$filename='File:         C:\temp\perf_monitor_02280905.csv';
if ($filename =~ m/File:\s+(\w:.*)/) {
   $filename = $1;
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Similar threads

Replies
2
Views
346
  • Locked
  • Question Question
Replies
4
Views
467
  • Locked
  • Question Question
Replies
1
Views
304
  • Locked
  • Question Question
Replies
5
Views
447

Part and Inventory Search

Sponsor

Back
Top