Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Perl RE; how to capture, and replace based on a block?

by taint (Chaplain)
on Dec 18, 2013 at 05:51 UTC ( #1067594=note: print w/ replies, xml ) Need Help??


in reply to Re: Perl RE; how to capture, and replace based on a block?
in thread Perl RE; how to capture, and replace based on a block?

Sorry. I just

cat ./FILE.html | perl {...}
in an open xterm. After several failures, and no more ideas. I closed the xterm, and asked for help. I didn't think it'd be of any use in the request.

I've since read every single reference in the Perl documentation, and while I think I've got the RE part down. I'm quite sure I don't know how to feed Perl the file properly to do any more than eat a single line at a time.

So let me have another go at it. The following

#!/usr/bin/perl -w #retest.pl # my feeble attempt to a multi-line RE in Perl $regexp = shift; while (<>) { print if /$regexp/; }
won't work as
# ./retest.pl \</\div\>\n\<\/body\> ./FILE.html
because shift will only manage input one line at a time. Attempts to figure how to make use of psed, and s2p, have failed miserably.

Apologies for the previous noise, and thank you for the thoughtful responses.

--Chris

Yes. What say about me, is true.


Comment on Re^2: Perl RE; how to capture, and replace based on a block?
Select or Download Code
Re^3: Perl RE; how to capture, and replace based on a block?
by hdb (Prior) on Dec 18, 2013 at 07:29 UTC

    Hi Chris, specifying a regex on the command line seems a difficult thing to do. At least you should be printing your $regexp to see what it contains.

    In any case, this code seems to work:

    my $str = " </div> </body> "; print "Success\n" if $str =~ /\<\/div\>\n\<\/body\>/;

    which suggests that if you slurp in your whole file as a single string (e.g. by unsetting $/), your regex should do its job.

    local $/; my $str = <>; print "Success\n" if $str =~ /\<\/div\>\n\<\/body\>/;
      Perl said; Success.

      Thanks a million, hdb! Your suggestion has helped me greatly in putting the last piece in my current "puzzle".

      Thanks again. I'd like to buy a round of +'s, for the house.

      --Chris

      UPDATE; I forgot to mention. The reason I was feeding the file to Perl is
      1) That's what worked best for me with sed.
      2) It seemed the easiest way to experiment getting a correct match with Perl.
      Yes. What say about me, is true.
      
Re^3: Perl RE; how to capture, and replace based on a block?
by QM (Vicar) on Dec 19, 2013 at 09:41 UTC
    For fiddling with little bits of code, just use the debugger straight away:
    swedish_chef> perl -demo Loading DB routines from perl5db.pl version 1.32 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): mo DB<1> $string = "one two three four" DB<2> x $string =~ m/(\w+)/g 0 'one' 1 'two' 2 'three' 3 'four'

    Note that "my" variables don't work as expected, I think they get created in the Debug scope, and not in the interpreted scope. But otherwise, have fun in the sandbox.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067594]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2014-10-22 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (122 votes), past polls