CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

Post #1 of 4 (4291 views)
Regex to find a match and extract the first value of a line Can't Post

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

Post #2 of 4 (4272 views)
Re: [kadu] Regex to find a match and extract the first value of a line [In reply to] Can't Post

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

Post #3 of 4 (4270 views)
Re: [kadu] Regex to find a match and extract the first value of a line [In reply to] Can't Post

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

Post #4 of 4 (4267 views)
Re: [BillKSmith] Regex to find a match and extract the first value of a line [In reply to] Can't Post

Bill,

That did the trick.


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


Thank you.
Kadu

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives