Regular Expression Query

Discussion in 'Perl Misc' started by George, Jan 24, 2005.

  1. George

    George Guest

    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
    George, Jan 24, 2005
    #1
    1. Advertising

  2. George wrote:
    > 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().
    Brian McCauley, Jan 24, 2005
    #2
    1. Advertising

  3. George <> wrote:

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 24, 2005
    #3
  4. George

    George Guest

    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#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
    George, Jan 24, 2005
    #4
  5. 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#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.

    <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
    Gunnar Hjalmarsson, Jan 24, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lonelyplanet999

    Regular Expression Query

    lonelyplanet999, Aug 14, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,823
  2. VSK
    Replies:
    2
    Views:
    2,283
  3. Sriv Chakravarthy
    Replies:
    0
    Views:
    791
    Sriv Chakravarthy
    Sep 18, 2003
  4. Martin Biddiscombe

    Regular expression query

    Martin Biddiscombe, Feb 3, 2006, in forum: Python
    Replies:
    4
    Views:
    368
    bruno at modulix
    Feb 6, 2006
  5. Replies:
    6
    Views:
    654
Loading...

Share This Page