Hello Tobias.
In my opinion it could be a bad idea to define a delimiter as the only
delimiter to use in your components.
As you said in your email you can use different delimiters for your regexp
depending on what you have to escape.
Personnaly I am a big fan of '#' [ preg_match('#(foo-bar)#', $var, $subj) ]
because I find it very readable and make you able to see immediatly
what your pattern is like.
I would recommend to adapt the choice of your delimiter to your context, for
example if you need to search for braces you can use '#' (or '/') as a delimiter
so you will not have to escape more than it is needed.
The main problem with regexes is not to find it but to maintain it so if you
can help you (or your colleagues) with a more readable pattern (and thus with
and adapted delimiter) just go for it.
Another point with regex, it is pretty useful to use the 'x' modifier which
allows comments in your pattern, like this for example :
$pattern = '/
[a-z] #my first comment
[0-9] #my second comment
/x'
Of course if you use the 'x' modifier you will not be able to use '#' as a
modifier ;)
Hope that helps.
Jérôme :)
Tobias Schlitt a écrit :
> Hi all!
>
> Kore and me just had an interessting discussion about the delimiter
> characters to be used in PCRE statements. The most common way (at least
> what I experienced) is to use slashes as delimiters, which results in
> constructs like the following one:
>
> preg_match( '/foo.*bar/i', $subj, $matches );
>
> In case I need to use a slash inside the regex itself, I need to escape
> it, thought. Therefore, if I know I need multiple slashes inside the
> regex, I tend to switch the delimiters to @ chars, to save me from
> escaping slashes. Still, then I need to escape any @ inside the regex:
>
> preg_match( '@/foo/[EMAIL PROTECTED]@i', $subj, $matches );
>
> Kore just told me, that he has switched to using normal braces as
> delimiters, which I did not know to be working at all:
>
> preg_match( '(/foo/[EMAIL PROTECTED])i', $subj, $matches );
>
> While this looks strange (at least to me) at the first glance, using
> braces seems to have several advantages over all other characters:
>
> - You don't need to escape braces inside the regex, if you use braces as
> a delimiter, resulting in no more silly escaping of the delimiter chars
> at all.
> - In $matches[0] the whole match is saved, while all higher indexes
> contain deeper matching braces. Using braces as delimiters make this
> behaviour more logical.
>
> While I'm almost convinced of using braces, I want to hear your opinions
> here. Kore and me want to use that style of delimiting regex in Webdav
> and I'm also thinking about migrating my other components regexes (if
> there are any). Any comments?
>
> Regards,
> Toby
--
Jérôme Renard
Technical consultant
[EMAIL PROTECTED] | eZ systems France | http://ez.no
--
Components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/components