Home > Archive > PERL Beginners > January 2006 > Quick regex question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Quick regex question
|
|
|
| Hi list,
I am not sure if there is a proper name for this but was having some
difficulty searching for it.
Basically I have data in a file that is between two different
characters, for example:
# data data
data
data data data *
# more dataaa
moreeee *
Basically I want to slurp that file in, then search for # and extract
all the data up to the * and print it, new line, next set of data.
Any pointers would be great.
Thanks!
| |
| Chas Owens 2006-01-24, 6:56 pm |
| On 1/24/06, Chris <chrisp3623@comcast.net> wrote:
> Hi list,
>
> I am not sure if there is a proper name for this but was having some
> difficulty searching for it.
>
> Basically I have data in a file that is between two different
> characters, for example:
>
> # data data
> data
> data data data *
> # more dataaa
> moreeee *
>
> Basically I want to slurp that file in, then search for # and extract
> all the data up to the * and print it, new line, next set of data.
>
> Any pointers would be great.
>
> Thanks!
>
I think what you are looking for is the $/ variable. Whatever you set
it to is what the daimond operator uses to delimit records (by default
it is \n). See "perldoc perlvar" for more info.
| |
| John Doe 2006-01-24, 6:56 pm |
| Chris am Dienstag, 24. Januar 2006 22.35:
> Hi list,
>
> I am not sure if there is a proper name for this but was having some
> difficulty searching for it.
>
> Basically I have data in a file that is between two different
> characters, for example:
>
> # data data
> data
> data data data *
> # more dataaa
> moreeee *
>
> Basically I want to slurp that file in, then search for # and extract
> all the data up to the * and print it, new line, next set of data.
I assume you mean that the whitespace after '#' and before '*' is not part of
the data (else, remove the \s* in the regex below).
The following just one (of the shorter) way to do it; read the expression
beginning with 'print' bottom-up:
======
#!/usr/bin/perl
use strict;
use warnings;
local $/;
my $content=<DATA>;
print join "\n",
map { do {$_=~s/\n/ /gs; $_} }
$content=~/#\s*(.*?)\s*\*/gs;
__DATA__
# data data
data
data data data *
# more dataaa
moreeee *
======
see
perldoc -f map
perldoc -f do
perldoc perlre
and for opening/closing an external file:
perldoc -f open
perldoc -f close
hth, joe
| |
| John W. Krahn 2006-01-24, 6:56 pm |
| Chris wrote:
> Hi list,
Hello,
> I am not sure if there is a proper name for this but was having some
> difficulty searching for it.
>
> Basically I have data in a file that is between two different
> characters, for example:
>
> # data data
> data
> data data data *
> # more dataaa
> moreeee *
>
> Basically I want to slurp that file in, then search for # and extract
> all the data up to the * and print it, new line, next set of data.
>
> Any pointers would be great.
From your example it looks like you could set the Input Record Separator to
something like "*\n".
John
--
use Perl;
program
fulfillment
|
|
|
|
|