Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > matching information between 2 words

Reply
Thread Tools

matching information between 2 words

 
 
timgerr()@()gmail.com
Guest
Posts: n/a
 
      10-04-2005
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      10-04-2005
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

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      10-04-2005
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
 
Reply With Quote
 
timgerr()@()gmail.com
Guest
Posts: n/a
 
      10-04-2005
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

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-04-2005
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

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      10-04-2005
Jim Gibson wrote:
> In article <(E-Mail Removed). com>,
> 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 ||
> Cancelediscuss 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
 
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
Re: Words and non-words, according to Microsoft et al Steve B NZ Computing 11 03-21-2008 11:52 PM
Replace stop words (remove words from a string) BerlinBrown Python 6 01-17-2008 02:37 PM
Words Words utab C++ 6 02-16-2006 07:00 PM
Non-noise words are incorrectly recognised as noise words. Peter Strĝiman ASP .Net 1 08-23-2005 01:26 PM
Re: A little bit of help regarding my linked list program required. - "words.c" - "words.c" Richard Heathfield C Programming 7 10-05-2003 02:38 PM



Advertisments