Reputation: 1
I wrote a regular expression for matching php's constant definition.
Example:
define('Symfony∞DI', SYS_DIRECTORY_PUBLIC . SYS_DIRECTORY_INCLUDES . SYS_DIRECTORY_CLASSES . SYS_DIRECTORY_EXTERNAL . 'symfony/di/');
Here is the regular expression:
define\((\"|\')+([\w-\.-∞]+)+(\"|\')+(,)+((\s)+(\"|\')+([\w-(\')-\\"-\.-∞-\s-(\\)-\/]+)+(\"|\')|(([\w-\s-\.-∞-(\\)-\/]+)))\);
When I executed with ActionScript it works fine. But when I executed with C# it gives me the following error:
parsing "define\((\"|\')+([\w-\.-∞]+)+(\"|\')+(,)+((\s)+(\"|\')+([\w-(\')-\\"-\.-∞-\s-(\\)-\/]+)+(\"|\')|(([\w-\s-\.-∞-(\\)-\/]+)))\);" - Cannot include class \s in character range.
Could you help me resolve this issue?
Upvotes: 0
Views: 484
Reputation:
See fge's answer for the error you're having. Without knowing what your tring to do and not deviating too much from your original, here is an alternative regex:
define\(\s*(["'])\s*[\w.∞]+\s*\1(?:\s*[.,]\s*(["']?)\s*[\w/]+\s*\2)*\s*\);
define
\(
\s* (["'])
\s* [\w.∞]+
\s* \1
(?:
\s* [.,]
\s* (["']?)
\s* [\w/]+
\s* \2
)*
\s*
\);
Upvotes: 1
Reputation: 121790
You seem to be using regexes in a completely convoluted way:
character classes: the -
is special and it there to compute an interval; I guess you have an ordering inversion which .Net doesn't handle whereas PHP handles it (or maybe the collating order is different in PHP). Your character class should read [\w.∞]
instead of [\w-.-∞]
, just to quote the first example;
no need to put a group around \s
: \s+
, not (\s)+
; similarly, ,
instead of (,)
.
'
is not special in a regex, and if you want to match two characters, use a character class, not a group + alternative: ['\"]
instead of (\'|\")
-- and note that the '"' is escaped only because you are in a doubly quoted string;
your regex is not anchored at the beginning and it looks like you want to match define
at the beginning of the output: ^define
and not define
.
The 1. is probably the source of your problems.
Rewriting your regex with all of the above gives this (in double quotes):
"^define\(([\"'][\w.∞]+[\"'],(\s+[\"']+[\w'\".∞\s\\/]+)+[\"']|([\w\s.∞\\/]+))\);"
which definitely doesn't look that it will ever match your input...
Try this instead:
"^define\(\s*(['\"])[\w.∞]+\1\s*,\s*([\w/]+(\s*\.\s*[\w/]+)*\s*\);$"
Upvotes: 2