matching information between 2 words

Discussion in 'Perl Misc' started by timgerr()@()gmail.com, Oct 4, 2005.

  1. I have a list of information that starts with <BEGIN> ....... <END>. I
    need to get the information that is located between the 2 words, having
    troubles with this because the <BEGIN> and <END> are not on the same
    lines. Any information would be a big help.

    Thanks

    Timgerr
    timgerr()@()gmail.com, Oct 4, 2005
    #1
    1. Advertising

  2. timgerr()@()gmail.com

    Paul Lalli Guest

    timgerr()@()gmail.com wrote:
    > I have a list of information that starts with <BEGIN> ....... <END>. I
    > need to get the information that is located between the 2 words, having
    > troubles with this because the <BEGIN> and <END> are not on the same
    > lines. Any information would be a big help.


    Please show some code that demonstrates the problems you are having.
    Have you read the Posting Guidelines for this group?

    Please make an attempt to help yourself before asking hundreds of other
    people to help you, at the very least by reading the Frequently Asked
    Questions that come with Perl. Have you read the Posting Guidelines
    for this group?

    perldoc -q "different lines"
    Found in /opt/perl/lib/5.6.1/pod/perlfaq6.pod
    How can I pull out lines between two patterns that are
    themselves on different lines?

    Paul Lalli
    Paul Lalli, Oct 4, 2005
    #2
    1. Advertising

  3. timgerr()@()gmail.com wrote:
    > I have a list of information that starts with <BEGIN> ....... <END>. I
    > need to get the information that is located between the 2 words, having
    > troubles with this because the <BEGIN> and <END> are not on the same
    > lines. Any information would be a big help.



    perl -ne'
    next unless /<BEGIN>/;
    redo unless ( $_ .= <> ) =~ /<BEGIN>(.*?)<END>/s;
    print $1;
    ' yourfile


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Oct 4, 2005
    #3
  4. Sorry, I have done so, I hate regular expressions. Here is the code
    that I have
    Code:
    $mystring = "<BEGIN>f2664be1b30679426ff62a6df9643640 || 12/13/2004 ||
    10:00:00 AM || 11:00:00 AM || 11:00:00 AM || duration || Canceled:
    Discuss Location Move of IT ||  ||  || 15 <END>";
    $test = $mystring =~ (m/<BEGIN>(.*)<END>/);
    print $mystring . "\n";
    print $test . "\n";
    
    I am unable to take the information located within $mystring and add it
    to a new variable.

    Thanks
    timgerr()@()gmail.com, Oct 4, 2005
    #4
  5. timgerr()@()gmail.com

    Paul Lalli Guest

    timgerr()@()gmail.com wrote:
    > Sorry, I have done so


    You have done what? Please quote some context when replying.

    > I hate regular expressions.


    A rather poor attitude to have, IMO.

    > Here is the code that I have
    >
    Code:
    > $mystring = "<BEGIN>f2664be1b30679426ff62a6df9643640 || 12/13/2004 ||
    > 10:00:00 AM || 11:00:00 AM || 11:00:00 AM || duration || Canceled:
    > Discuss Location Move of IT ||  ||  || 15 <END>";
    > $test = $mystring =~ (m/<BEGIN>(.*)<END>/);
    > print $mystring . "\n";
    > print $test . "\n";
    > 


    Does your code have actual embedded newlines in the string? I can't
    tell if it does or if it's just the result of line-wrapping in your
    post.

    A pattern match in scalar context returns true or false (by which I
    mean the number 1 or the empty string). If you want to return the
    values captured by ( ), you have to assign in list context:
    my ($test) = $mystring =~ m/<BEGIN>(.*)<END>/;

    If you do indeed have embedded newlines in your string, you need to
    recall that the . wildcard matches "anything except the newline". If
    you want it to also match the newline character, you need to add the /s
    modifier:
    my ($test) = $mystring =~ m/<BEGIN>(.*)<END>/s;

    Paul Lalli
    Paul Lalli, Oct 4, 2005
    #5
  6. Jim Gibson wrote:
    > In article <>,
    > timgerr <"timgerr()@()gmail.com"> wrote:
    >
    >>Sorry, I have done so, I hate regular expressions.

    >
    > Then don't use them:
    >
    > #!/usr/local/bin/perl
    > use strict;
    > use warnings;
    >
    > my $mystring = "<BEGIN>f2664be1b30679426ff62a6df9643640 || 12/13/2004
    > || 10:00:00 AM || 11:00:00 AM || 11:00:00 AM || duration ||
    > Canceled:Discuss Location Move of IT || || || 15 <END>";
    >
    > my $start = index($mystring,'<BEGIN>');
    > my $end = index($mystring,'<END>');
    > if( $start != -1 && $end != -1 ) {


    You should also verify that $start < $end or the following code won't work.

    > $start += 7; # skip <BEGIN>
    > my $test = substr($mystring,$start,$end-$start);
    > print "test: $test\n";
    > }



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Oct 4, 2005
    #6
    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. Peter Strøiman
    Replies:
    1
    Views:
    2,070
    Peter Strøiman
    Aug 23, 2005
  2. Richard Heathfield
    Replies:
    7
    Views:
    350
    Barry Schwarz
    Oct 5, 2003
  3. utab

    Words Words

    utab, Feb 16, 2006, in forum: C++
    Replies:
    6
    Views:
    415
    Daniel T.
    Feb 16, 2006
  4. BerlinBrown
    Replies:
    6
    Views:
    4,430
  5. Lasse Edsvik

    replace words with bold words

    Lasse Edsvik, Oct 5, 2003, in forum: ASP General
    Replies:
    9
    Views:
    228
Loading...

Share This Page