You are here: Re: [PHP] preg_match « PHP « IT news, forums, messages
Re: [PHP] preg_match

Posted by Robin Vickery on 08/23/05 11:58

On 8/23/05, Richard Lynch <ceo@l-i-e.com> wrote:
> The advantage that when I have to change the string to match the
> new/altered data, and that data has, oh, I dunno, something like ' or
> \ or 0, 1, 2, 3, ...9 in it, and then my Regex suddenly doesn't work
> because \abc7 won't work the same as \7abc but \\abc7 works the same
> as \\7abc

Ummm... no it doesn't.

Both of these will fail to compile:
preg_match( '/\\7abc/', $myString);
preg_match( '/\7abc/', $myString);

Both of these are fine:
preg_match( '/\\abc7/', $myString);
preg_match( '/\abc7/', $myString);

In fact they behave *exactly* as if you hadn't doubled them up. You've
not achieved anything, but you've made your regexp just a little more
difficult to read and maintain.

> I guess I just like things that ALWAYS work instead of things that
> SOMETIMES work. Silly me.
>
> \\ always works
> \ only works sometimes, depending on what character follows it.

No it doesn't. Indiscriminately doubling up all your back-slashes is
not an alternative to actually learning the proper syntax.

> PS
> A Regex is unreadable code by definition anyway.
>
> \\ isn't any more unreadable, with practice, than \, especially if you
> are CONSISTENT and use it in PHP all the time, not just in Regex.

Yeah, "with practice" you can get used to anything... Unfortunately
I'm not the only developer here. There are three teams of programmers
who would take umbrage with any need to practice before reading my
code.

>
> But the sheer number of weird character combinations and positional
> difference (^ inside [] and ^ outside [] and ^ at the beginning of
> either are all COMPLETELY different) make Regex unreadable no matter
> how long you stare at it.

With practice...

No, you're absolutely right. The way operators change meaning
depending on context can be confusing, especially when you're getting
started. They're addressing this already with the design of Perl 6. No
doubt it'll trickle through to PHP in due course.

http://dev.perl.org/perl6/doc/design/apo/A05.html#Too_compact_and_"cute"

But in the mean time, it's not *that* hard to learn the syntax of
regular expressions. OK, they're rather terse. But apart from that,
they're a lot simpler than a proper language like PHP and you don't
seem to have problems with that.

> I dunno what masochist invented this Regex crap, but he oughta be shot

Computational Linguists, first up against the wall, come the revolution.

-robin

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация