Justtechjobs.com Find a programming school near you






Online Campus Both


php3-list | 2000051

Re: [PHP3] Syntax Highlight From: Alfred Perlstein (bright <email protected>)
Date: 05/01/00

* Vincent Driessen <vinnie.driessen <email protected>> [000501 09:35] wrote:
> Dear Alfred,
>
> Thank you very much for your great help on my syntax highlighting problem. I
> now realise that the regex functions are very powerful ones, and therefor
> very hard to understand. In your mail, you said "pick up a book on regex",
> but I don't have one, and the PHP manual doesn't go this deep. I would love
> to learn more about this cool function. Would you please explain how your
> function works or mail me a few clear manuals or sites where I can learn
> this? I know THAT it works, but I don't know how to read it.
>
> There is only one problem with your function. It goes wrong when you type
> this as input:
>
> $input = "function MyFunction(Foo: string);";
>
> $output =
> ereg_replace("([^A-Za-z_]|^)(((function|string)([^A-Za-z0-9_]|$))+)",
> "\\1<b>\\2</b>", $input);
>
> echo $output;
>
> This one outputs <b>function</b> MyFunction(Foo: <b>string)</b>;
>
> Notice the ")" inside the <b>-tags, instead of outside them. Why does this
> go wrong?
>
> Again, thank you very much.

I hope you don't mind me cc'ing the general lists again to answer this
because I'd hope if someone else notices a problem they'll let us know. :)

Yeah, I made a mistake in that regex... it should be:

 ereg_replace("([^A-Za-z0-9_]|^)(((function|string)([^A-Za-z0-9_]|$))+)",
                        "\\1<b>\\4</b>\\5", $input);

=====

Here's the deal until you get your books:

([^A-Za-z0-9_]|^)
I want to find a pattern that starts with either the beginning of the
line '^' or a non-alphanumeric [^A-Za-z0-9_].

(((function|string)([^A-Za-z0-9_]|$))+)
followed by one or more occurances of:
   ((function|string)([^A-Za-z0-9_]|$))

(function|string)
which is the word "function" or the word "string"

([^A-Za-z0-9_]|$)
which is then followed by either the end of a line '$' or another non
alphanumeric '[^A-Za-z0-9_]'

=====

ok, now for the replacement...

Then I want to replace the expression like so...

"\\1<b>\\4</b>\\5"

which means.....

start counting parenthases starting from the left:

the first thing in parens in my non-alphanumeric, so I just want to leave it
as is so I just use \\1.

Now i want to surround my function with bold tags, so I count until i hit
the bold tags... which is 4, so:
  \\1<b>\\4

I could have just used "function" instead of '\\4' but then you could only
do one keyword at a time, by using \\4 you can put more than one word
in that group (seperated by |) is the regex will then replace \\4 with
the word it matched.

Then I want to put the last non-alphanumeric back into the string and it
is the fifth paren'd pattern so i finish off with:
  "\\1<b>\\4</b>\\5"

=====

I appreciate you getting back to me on how to do this because I probably
would have used my own broken code had you not pointed out that bug.

As far as books, I think ORA has some books on sed and awk as well as
"mastering regular expressions" that are really good texts on the
sbuject.

Best of luck,

-- 
-Alfred Perlstein - [bright <email protected>|alfred <email protected>]
"I have the heart of a child; I keep it in a jar on my desk."

-- PHP 3 Mailing List <http://www.php.net/> To unsubscribe, send an empty message to php3-unsubscribe <email protected> To subscribe to the digest, e-mail: php3-digest-subscribe <email protected> To search the mailing list archive, go to: http://www.php.net/mailsearch.php3 To contact the list administrators, e-mail: php-list-admin <email protected>