matching information between 2 words

T

timgerr()

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
 
P

Paul Lalli

timgerr()@()gmail.com said:
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
 
J

John W. Krahn

timgerr()@()gmail.com said:
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
 
T

timgerr()

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
 
P

Paul Lalli

timgerr()@()gmail.com said:
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
 
J

John W. Krahn

Jim said:
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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top