|
Posted by "Richard Lynch" on 08/23/05 09:58
On Mon, August 22, 2005 8:13 am, Robin Vickery wrote:
> On 8/22/05, Richard Lynch <ceo@l-i-e.com> wrote:
>> On Sat, August 20, 2005 5:00 am, John Nichel wrote:
>> > Personally, I have never used \\ in PCRE when looking for things
>> like
>> > spaces (\s), word boundraries (\b), etc. and it's all worked out
>> fine.
>>
>> Personally, {
>> I
>> } have
>> never {
>> used
>> proper
>> indenting
>> in
>> my
>> code } and
>> it's
>> all
>> worked
>> out
>> fine;
>>
>> :-)
>
> Unnecessary backslashes make your regular expressions almost as
> unreadable as those indents.
>
> You only ever need to escape a backslash in single-quotes if it's
> before another backslash or a single-quote.
>
> In fact the manual itself explicitly says this:
>
> http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single
>
> So what benefit exactly do you see in doubling up all the backslashes
> in a single-quoted regexp? It's certainly not helping readability, and
> they don't actually do anything...
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
I have to MAINTAIN this code, and when the input changes, as it
inevitably will, I do *NOT* want to sit down and figure out that hairy
Regex again. It was bad enough the first time. Hell, I had to spend
a couple hours building it up iteratively to get it right.
If I use \\ when I want \, then it will ALWAYS be correct, no matter
what character I have to shove in after it to match "The New Data".
If I use \ when I want \, maybe it works sometimes, maybe it doesn't
when I have to match "the new data". (\t is tab)
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.
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.
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.
I dunno what masochist invented this Regex crap, but he oughta be shot
:-)
* I actually LIKE Regex when it's simple... But, man, you start
adding in a lot of complexity, and it gets really ugly really fast.
Oh well. It works.
--
Like Music?
http://l-i-e.com/artists.htm
Navigation:
[Reply to this message]
|