Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regular Expression Query

Reply
Thread Tools

Regular Expression Query

 
 
George
Guest
Posts: n/a
 
      01-24-2005
Hi,
I have following lines in a file, below is example of one such line ,
yes it is long line


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

What I need to get is
MediaRecorder-NewYork#xenaroject: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
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      01-24-2005


George wrote:
> I have following lines in a file, below is example of one such line ,
> yes it is long line
>
>
> 9) MediaRecorder-NewYork#xenaroject:BBM#1 working xena
> project MediaRecorder BBM51#1 NewYork#541
>
> What I need to get is
> MediaRecorder-NewYork#xenaroject: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().

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-24-2005
George <(E-Mail Removed)> wrote:

> I have following lines in a file, below is example of one such line ,
> yes it is long line
>
>
> 9) MediaRecorder-NewYork#xenaroject:BBM#1 working xena
> project MediaRecorder BBM51#1 NewYork#541
>
> What I need to get is
> MediaRecorder-NewYork#xenaroject:BBM#1



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

my(undef, $to_get) = split;


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      01-24-2005
Brian McCauley wrote:

>
>
> George wrote:
> > I have following lines in a file, below is example of one such line
> > , yes it is long line
> >
> >
> > 9) MediaRecorder-NewYork#xenaroject:BBM#1 working xena
> > project MediaRecorder BBM51#1 NewYork#541
> >
> > What I need to get is
> > MediaRecorder-NewYork#xenaroject: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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-24-2005
George wrote:
>>George wrote:
>>>I have following lines in a file, below is example of one such line
>>>, yes it is long line
>>>
>>>9) MediaRecorder-NewYork#xenaroject:BBM#1 working xena
>>>project MediaRecorder BBM51#1 NewYork#541
>>>
>>>What I need to get is
>>>MediaRecorder-NewYork#xenaroject: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.

<snip>

> 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;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments