Hans Meier (John Doe) wrote:
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)

Sorry, I'm relatively new to programming in general (perl is my first programming language), so I'm not sure what you mean by "sanitizing." Does this imply having the program correct any possible errors in the imput to be compliant with the way the program operates on the input?

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.

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!

Thanks for mentioning the '$/' variable, which is probably what I was looking for but didn't know existed (yeah, I know, RTF...)!

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?)

The while is reading in a file, so it is in the while(<FILEHANDLE>) context. Here is the relevant (paraphrased) code:

        print OUT "<p>";
                while (defined($lines = <IN>)) {
                ...make the substitutions to the text...
                print OUT $lines;
        }
        print OUT "</p>";


The p-tags are there because I want each list of links to be an HTML paragraph formatted according to my CSS definition of <p>. Nothing special.

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".

Thank you. Nothing helps a community more than communication.

Adam



--
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