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

RegEx wizard help needed

Status
Not open for further replies.

webfuture

MIS
Jan 17, 2003
65
CA
I have this long string
/usr/lib/vmware/bin/vmware-vmx -C /vmware/vm-machines/vm-cs-xp-84/xp_pro_english_base.vmx -@ ""

And need to print only this part "vm-cs-xp-84" inside it.

What regex could do that. I know it's doable but not with my limited knowledge. What I would do and want to avoid is multiple splits.

Thanks in advance.

Simon
 
one way - fine if your directory path doesn't change...

Code:
[b]#!/usr/bin/perl[/b]

$_ = '/usr/lib/vmware/bin/vmware-vmx -C /vmware/vm-machines/vm-cs-xp-84/xp_pro_english_base.vmx -@ ""';

@components = split(/\//);

print $components[8];


Kind Regards
Duncan
 
Here's a regex that should work for you:
Code:
my $str = '/usr/lib/vmware/bin/vmware-vmx -C /vmware/vm-machines/vm-cs-xp-84/xp_pro_english_base.vmx -@ ""';
$str =~ m!vm-machines/([^/]+)! && print "$1\n";
Using split would really work just fine too, you don't need more than one:
Code:
my @fields = split '/', $str;
print "$fields[8]\n";
 
Yeah, what Duncan said! (Looks like I was a bit slow.)
 
;-)

... a bit neater with rharsh's literal '/' though


Kind Regards
Duncan
 
Thanks to all... That's so easy....

I do perl 1 week per year and regex 1 hour per year... Everytime I go back to this I get lost.

I have used the split, it will be easier to debug in the long run, since I am no expert on Regex

Thanks again

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top