 |
Anaristos Sorcerer
Joined: 17 Jul 2007 Posts: 821 Location: California
|
Posted: Sun Feb 20, 2011 8:40 am
[Pro333a] Problem with %regex and strings containing parens |
| Code: |
$string = "This is a (test)"
;;
$count = 0
;;
$index = %regex( %replace( %replace( $string, "(", "\("), ")", "\)"), "(?i)^\s*([^,]+),?\s*([^$]+)?", $room, $zone, $count)
;;
#PRINT $room
|
RESULT:
EXPECTED RESULT:
The returned value is having the parens stripped. This happens because of variable expansion, but there is no way to avoid that at the moment.
The peculiar thing is that it doesn't matter what I do to the parens:
I can leave them as is.
I can use the regex escape (\).
I can use the cmud escape (~).
In all 3 cases the result is the same.
The regex pattern has been tested on outside regex viewers and it is correct. Using those viewers, I get the correct string returned.
In fact since the parens is in the input string and not in the regex pattern there is no need to use the \. So the "correct" escape character would be ~. But using it doesn't make any difference.
The other possible ways of doing it would be:
| Code: |
$index = %regex( $string, "(?i)^\s*([^,]+),?\s*([^$]+)?", $room, $zone, $count)
//
// OR
//
$index = %regex( %replace( %replace( $string, "(", "~("), ")", "~)"), "(?i)^\s*([^,]+),?\s*([^$]+)?", $room, $zone, $count)
|
|
|
_________________ Sic itur ad astra. |
|
|
 |
MattLofton GURU
Joined: 23 Dec 2000 Posts: 4834 Location: USA
|
Posted: Sun Feb 20, 2011 8:00 pm |
Try adding more quotes to the assigned value. The first set of quotes gets stripped away during the assignment, and since () unquoted equate to evaluation...
|
|
_________________ EDIT: I didn't like my old signature |
|
|
 |
Anaristos Sorcerer
Joined: 17 Jul 2007 Posts: 821 Location: California
|
Posted: Sun Feb 20, 2011 9:32 pm |
I see... What we need is a %noparse function.
EDIT: While on the subject of noparse... I ran the script with the Parse Arguments box unchecked. I did receive the string as is, however the script acted as it did before. I vaguely remember why the option was put in, so perhaps someone can clarify it for me? Maybe this is the route to take. |
|
_________________ Sic itur ad astra. |
|
|
 |
Tech GURU

Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Mon Feb 21, 2011 6:22 pm |
Note sure what version you are running but I got when I ran it.
|
|
_________________ Asati di tempari! |
|
|
 |
Anaristos Sorcerer
Joined: 17 Jul 2007 Posts: 821 Location: California
|
Posted: Tue Feb 22, 2011 12:04 am |
I am running 333aPro.
If I uncheck ParseArguments I can also get the string you showed, that doesn't do me any good, though. The regex basically allows for 0 to 2 comma-separated parameters to be passed to the script. This is done precisely because I don't want to worry about encasing the parameters in double quotes so they can be distinguished. The processor of the parameters doesn't want "....\(...\)....", or rather, it is trying to match an exact/partial string and the escape characters interfere with that. |
|
_________________ Sic itur ad astra. |
|
|
 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|