Regular Expression Query

G

George

Hi,
I have following lines in a file, below is example of one such line ,
yes it is long line


9) MediaRecorder-NewYork#xena:project:BBM#1 working xena
project MediaRecorder BBM51#1 NewYork#541

What I need to get is
MediaRecorder-NewYork#xena:project:BBM#1

What I have done so far is
my @prog=map (/[(.*?)A-Za-z](.*)(?=\s+ )/,@comfile); #where @comfile=
contents of file

Why does this regex fail to find the end of search= whitespace
if I use (?=working) it works but I get blank lines till working which
I dont need
 
B

Brian McCauley

George said:
I have following lines in a file, below is example of one such line ,
yes it is long line


9) MediaRecorder-NewYork#xena:project:BBM#1 working xena
project MediaRecorder BBM51#1 NewYork#541

What I need to get is
MediaRecorder-NewYork#xena:project:BBM#1

What I have done so far is
my @prog=map (/[(.*?)A-Za-z](.*)(?=\s+ )/,@comfile); #where @comfile=
contents of file

Why does this regex fail to find the end of search= whitespace
if I use (?=working) it works but I get blank lines till working which
I dont need

(?=\s+ )

Looks for one or more whitespace characters followed by an literal space
character. Perhaps all the whitespace in the file is tabs.

BTW [(.*?)A-Za-z] looks wrong to me. The characters ( . * ? and ) are
not special inside character classes.

Random shot in the dark you meant:

my @prog=map (/(\S+)/,@comfile);

It may be more ideomatic to use split().
 
T

Tad McClellan

George said:
I have following lines in a file, below is example of one such line ,
yes it is long line


9) MediaRecorder-NewYork#xena:project:BBM#1 working xena
project MediaRecorder BBM51#1 NewYork#541

What I need to get is
MediaRecorder-NewYork#xena:project:BBM#1


If the line is in $_, then this will do it:

my(undef, $to_get) = split;
 
G

George

Brian said:
I have following lines in a file, below is example of one such line
, yes it is long line


9) MediaRecorder-NewYork#xena:project:BBM#1 working xena
project MediaRecorder BBM51#1 NewYork#541

What I need to get is
MediaRecorder-NewYork#xena:project:BBM#1

What I have done so far is
my @prog=map (/[(.*?)A-Za-z](.*)(?=\s+ )/,@comfile); #where
@comfile= contents of file

Why does this regex fail to find the end of search= whitespace
if I use (?=working) it works but I get blank lines till working
which I dont need

(?=\s+ )

Looks for one or more whitespace characters followed by an literal
space character. Perhaps all the whitespace in the file is tabs.

BTW [(.*?)A-Za-z] looks wrong to me. The characters ( . * ? and )
are not special inside character classes.

Random shot in the dark you meant:

my @prog=map (/(\S+)/,@comfile);

It may be more ideomatic to use split().

My Idea of
/[(.*?)A-Za-z](.*)(?=\s+ )/
--W--|---X---|-Y-|--Z---
is
W=Anything from start in my case it is 1) ...100)...
X=Anything Starts from A or a
Y=Anything in between
Z= stops where space found is true
 
G

Gunnar Hjalmarsson

George said:
George said:
I have following lines in a file, below is example of one such line
, yes it is long line

9) MediaRecorder-NewYork#xena:project:BBM#1 working xena
project MediaRecorder BBM51#1 NewYork#541

What I need to get is
MediaRecorder-NewYork#xena:project:BBM#1

What I have done so far is
my @prog=map (/[(.*?)A-Za-z](.*)(?=\s+ )/,@comfile); #where
@comfile= contents of file

Why does this regex fail to find the end of search= whitespace

Because the (.*) part is greedy.

My Idea of
/[(.*?)A-Za-z](.*)(?=\s+ )/
--W--|---X---|-Y-|--Z---
is
W=Anything from start in my case it is 1) ...100)...
X=Anything Starts from A or a
Y=Anything in between
Z= stops where space found is true

This would better fit your idea:

/.*?\)\s+([A-Za-z].*?)(?=\s+)/

But please consider this idea instead:

my @prog = map
/
\s # last space char in the first group of space chars
(\S+) # non-space chars
/x, @comfile;

Or this:

my @prog = map { (split)[1] } @comfile;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top