Home: Perl Programming Help: Regular Expressions:
Regex to find a match and extract the first value of a line



kadu
New User

Apr 2, 2011, 6:25 AM


Views: 6489
Regex to find a match and extract the first value of a line

Hi,

I need a regex that looks for a match in a line and if found extract the first value of the line:

The list will look like this:


Code
0x1a0005c :     0   464   18x 436 :: -1 : "Pidgin" "Pidgin" : "Buddy List" : "buddy_list" 
0x2400003 : 1389 302 51x 598 :: -1 : "gkrellm" "Gkrellm" : "gkrellm" : "-NONE-"
0x1c00004 : 0 0 1440x 400 :: -1 : "stjerm" "Stjerm" : "stjerm" : "-NONE-"
0x1a00902 : 0 47 18x 415 :: -1 : "Pidgin" "Pidgin" : "Gustavo Erler" : "conversation"


I need to grep for a name (ex. stjerm) and if found give me the window ID, which is the first value in the line. On this case 0x1c00004

I've been trying this with grep and split but cannot get the result I want, instead grep is returning 1 (ie. it found a match).


Code
my ($id) = split (":", grep{/$app/} @wl);

Where $app contains the string I'm looking for (stjerm) and @wl contains the list of windows.

I'm sure it's possible to remove the split command and use a regex on grep that will match the string and then return me all contents before the first :

Thanks
Kadu


FishMonger
Veteran / Moderator

Apr 2, 2011, 10:15 AM


Views: 6470
Re: [kadu] Regex to find a match and extract the first value of a line

Since grep returns the entire string, you'd need to do this in 2 steps to capture the desired data.

Instead, it would be better to loop over the data and use a single regex.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use 5.010;

my $app = 'stjerm';

while( <DATA> ) {
say $1 if /^(\S+).+$app/;
}

__DATA__
0x1a0005c : 0 464 18x 436 :: -1 : "Pidgin" "Pidgin" : "Buddy List" : "buddy_list"
0x2400003 : 1389 302 51x 598 :: -1 : "gkrellm" "Gkrellm" : "gkrellm" : "-NONE-"
0x1c00004 : 0 0 1440x 400 :: -1 : "stjerm" "Stjerm" : "stjerm" : "-NONE-"
0x1a00902 : 0 47 18x 415 :: -1 : "Pidgin" "Pidgin" : "Gustavo Erler" : "conversation"



BillKSmith
Veteran

Apr 2, 2011, 10:23 AM


Views: 6468
Re: [kadu] Regex to find a match and extract the first value of a line

You need grep to run in list context.


Code
use strict; 
use warnings;
my $app = 'stjerm';
my @wl = (
'0x1a0005c : 0 464 18x 436 :: -1 : "Pidgin" "Pidgin" : "Buddy List" : "buddy_list"',
'0x2400003 : 1389 302 51x 598 :: -1 : "gkrellm" "Gkrellm" : "gkrellm" : "-NONE-"',
'0x1c00004 : 0 0 1440x 400 :: -1 : "stjerm" "Stjerm" : "stjerm" : "-NONE-"',
'0x1a00902 : 0 47 18x 415 :: -1 : "Pidgin" "Pidgin" : "Gustavo Erler" : "conversation"',
);
my ($id) = split (":", (grep{/$app/} @wl)[0]);
print $id, "\n";

Good Luck,
Bill


kadu
New User

Apr 2, 2011, 11:03 AM


Views: 6465
Re: [BillKSmith] Regex to find a match and extract the first value of a line

Bill,

That did the trick.


Code
Thunderbird:  0x480006d   
conversation: 0x1a00902
Geany: 0x4600003
Firefox: 0x4a0006d
buddy_list: 0x1a0005c
Gkrellm: 0x2400003


Thank you.
Kadu