multilinepattern match

S

Sunil

Hi,
I have to match a string like

"end;
/"

in a file. Any suggestions on how to do this?

Thanks,
Sunil.
 
T

Tad McClellan

Sunil said:
Hi,
I have to match a string like

"end;
/"

in a file. Any suggestions on how to do this?


All of these will match that string:

//

/./

/.*/s

/e/

/;/

/end/

/"/

/"end;\n\/"/

m#"end;\n/"#




1) post real Perl code as suggested in the Posting Guidelines.
I cannot tell if those double quotes are in the string or not...

2) tell us what it is about the string that you want to match on,
else any of the above will do what you have asked for.
 
S

Sara

Sunil said:
Hi,
I have to match a string like

"end;
/"

in a file. Any suggestions on how to do this?

Thanks,
Sunil.

look at the "s" and "m" switches in perl regexes, for starters they
are very useful for multiline inputs.

On this other hand you can do this match with no switches:

#!/usr/bin/perl -wd
$_ = "a bunch of
stuff
is here..
end;
/
thats all
folks";

print "Eureka!\n" if /end;\n\//;


************************************************************************

I guess would be one way?

-G
 
J

James Willmore

[email protected] (Sara) wrote in message news: said:
#!/usr/bin/perl -wd

Just a suggestion ...
when posting code, please avoid using the '-d' switch. I realize that
it throws one into the debugger, but others may not. Given the
various states of experience in Perl found in this newsgroup, this
could confuse the not so experienced reader.

Again - just a suggestion :)
$_ = "a bunch of
stuff
is here..
end;
/
thats all
folks";

Another suggestion is avoid setting '$_'. It's a special variable and
one should avoid setting it. Again - I understand what you're trying
to demonstrate, but those less experienced may not.

Again - just a suggestion ;-)

Jim
(jwillmore _at_ adelphia _dot_ net)
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,
I have to match a string like

"end;
/"

in a file. Any suggestions on how to do this?

/"end;
\/"/

or

m!"end;
/"!

What's so hard about that? :)
- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP7oOq2PeouIeTNHoEQJaZwCfdGQPZt0FTupWbyEqG59kExKCz7UAnA6A
Kg3jUe+AxPTRHmriU5VWMt1F
=+rYz
-----END PGP SIGNATURE-----
 
A

Anno Siegel

James Willmore said:
(e-mail address removed) (Sara) wrote in message


Just a suggestion ...
when posting code, please avoid using the '-d' switch. I realize that
it throws one into the debugger, but others may not. Given the
various states of experience in Perl found in this newsgroup, this
could confuse the not so experienced reader.

Again - just a suggestion :)


Another suggestion is avoid setting '$_'. It's a special variable and
one should avoid setting it. Again - I understand what you're trying
to demonstrate, but those less experienced may not.

There is nothing wrong with setting $_ in your main script when it's
handy (and it often is). Care must be taken when doing this in a
subroutine. Then you *must* localize $_ before setting it, or you're
in for some nasty surprise.

Anno
 
J

James Willmore

On 18 Nov 2003 12:24:28 GMT
There is nothing wrong with setting $_ in your main script when it's
handy (and it often is). Care must be taken when doing this in a
subroutine. Then you *must* localize $_ before setting it, or
you're in for some nasty surprise.

True. However, that wasn't done in the related post.

I guess it would have been better phrased:
Don't use $_ unless you know what you're doing.

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Parsley is gharsley. -- Ogden Nash
 
S

Sunil

@news.oracle.com:
/"end;
\/"/

or

m!"end;
/"!

What's so hard about that? :)
- --
Eric


The hard part is "how do I get the file contents (which may be huge) into a
string variable"
or is line by line from the file the only option?

Thanks,
Sunil.
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The hard part is "how do I get the file contents (which may be huge)
into a string variable"
or is line by line from the file the only option?

There are several ways. You may or may not have to read the entire file
into a string variable all at once. Tell us more about the problem you're
trying to solve, and what you expect the input data to look like, and we'll
see if we can't set you up.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP7teCGPeouIeTNHoEQLylACgji3DZaezrOwhKFZ7vk5pEfMmKRUAoKko
ca1VKTL1u9dW1ldXeYSeeedI
=9n/p
-----END PGP SIGNATURE-----
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top