Adam W am Dienstag, 7. März 2006 23.16:
> Hans Meier (John Doe) wrote:
> > just to sum up:
> >
> > $test =~ s{ (.*?)  \(  (.*?)  \) }
> >           {<a href="$2" alt="$2">$1</a>}xsg;
> >
> > - "\(" instead of "[(]": more readable
> > - no /m modifier       : unnecessary without ^/$-anchors
> > - /s                   : may be appropriate for your html source text
> >
>
> The context for this regexp is a simple program I wrote to turn text
> files of the form:
>        someword(http://www.linktosomething.com)
> to
>       <a href="http://www.linktosomething.com";
>               alt="http://www.linktosomething.com";>someword</a>
> (without the linebreaks, of course).
> It works by reading in some text file, line by line, using while(<>).

That's ok, so no /s required :-)

Then you can even check if what should be on one line is effectively on one 
line, by:

$text=~/your_regex_here/x or die "improper line format: $text";

(this dies on empty lines etc. too, and of course the input file's lines 
should be sanitizes before, and to be on the secure side, the transformation 
script should do that too... but all that depends on the exact circumstances 
you use the script)

> Initially, I wanted the program to stick a <p> tag at the beginning of
> the file and a </p> tag at the end of the file.

That's going in the direction of an XML formatted file.
In my opinion, if you want to do that, you could store the input file data as 
XML and use an XML Parser or XSLT to transform the input data. Then you would 
not need regexes, but the hole thing would be blown up a bit :-)

> Is it possible to make 
> a regexp that will only match the beginning and ending of a file within
> a "while" loop?

Not directly. I can't see at the moment a way to do that without ugly code.

But what should be the sense of that in conjunction with <p>/</p>??

> It seems as though you cannot, since "while" only reads 
> in data line by line and thus, even if you remove the newlines from the
> input, you can only operate on one line of input at a time.  
> Is this correct?  

Not in all cases, see following paragraph.

> If it is, then would it be correct to say that, practically 
> speaking, while in a "while" loop, the presence or absence of a /s
> modifier will not effect rexep recognition?

In the while (<FILENANDLE>) case, and $/ (perldoc perlvar) set to newline: 
yes.

No when slurping all lines at once into an array and loop over the array.

Also no if $/ is set to something that reads more than one line.

The $/ variable is very interesting for reading multiline records from a file 
like conventional lines. Have a look at it!

I always think of lines in the present context as "meta-lines" or "virtual 
lines" ;-)

> I eventually got around this problem by simply printing what I wanted
> after opening the file but before the while loop, and then printing
> again after the while loop had ended but before the next file is opened.

Sorry, this is not clear to me (what's the purpose of the while in this 
scenario?)

>     However, I remain curious about the function of /s in while loops.
>
> Just something I've been wondering about,

Nice way to post on this list; not the kind of "Oh, I spend 10 secs to ask, 
there will be people spending 1/2 h to solve my problem".
Thanks :-)

Hans

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to