"Turning Off" regular expressions?

Doc requests, organization, and submissions

Moderators: dorpond, trevor, Azhrei

Post Reply
Syne
Kobold
Posts: 17
Joined: Fri Sep 19, 2008 9:15 am

"Turning Off" regular expressions?

Post by Syne »

Can I turn off regular expressions for the replace() function? I need to replace a string with {} in it (a JSON object to be exact) and I think that's what's screwing up the output. When using the replace() function in this way:

Code: Select all

[H: people = "{'hello' : 'people'}"]
[R: replace(people, people, "5")]
I get the error:
MapTool wrote: net.rptools.parser.ParserException: Illegal repetition {"hello":"people"} error executing expression: replace(people, people, "5")
Edit: I've found a quick and dirty way to do this. By running?

Code: Select all

[H: people = "{{{{{{{{{{{{{{{'hello' : 'people'}}}}}}}}}}}}}}}}}"]
[H: people2 = replace(people, "\\{", "\\\\{")]
[R: replace(people, people2, "5")]
However, I dislike this. Does anyone have a better solution?

Craig
Great Wyrm
Posts: 2107
Joined: Sun Jun 22, 2008 7:53 pm
Location: Melbourne, Australia

Re: "Turning Off" regular expressions?

Post by Craig »

Syne wrote:Can I turn off regular expressions for the replace() function? I need to replace a string with {} in it (a JSON object to be exact) and I think that's what's screwing up the output. When using the replace() function in this way:

Code: Select all

[H: people = "{'hello' : 'people'}"]
[R: replace(people, people, "5")]
I get the error:
MapTool wrote: net.rptools.parser.ParserException: Illegal repetition {"hello":"people"} error executing expression: replace(people, people, "5")
Edit: I've found a quick and dirty way to do this. By running?

Code: Select all

[H: people = "{{{{{{{{{{{{{{{'hello' : 'people'}}}}}}}}}}}}}}}}}"]
[H: people2 = replace(people, "\\{", "\\\\{")]
[R: replace(people, people2, "5")]
However, I dislike this. Does anyone have a better solution?
There is no way to turn off regular expressions
Also in b51+ you are not able to treat a JSON object as a string
without doing string(jobj) first. But there is a better solution to the
problem for b51+
For b51+ the solution is to use json.evaluate().

Code: Select all

[H: people = "{'hello' : '{people}'}"]
<!-- do something ...-->
[h: people = 5]
[r: json.evaluate(people)]
[code]

When you call json.evaluate() it will try to evaluate any values in
the array or object against the current macros variables or impersonated
tokens properties.

Syne
Kobold
Posts: 17
Joined: Fri Sep 19, 2008 9:15 am

Re: "Turning Off" regular expressions?

Post by Syne »

Using replace() on JSON objects seems to work, though other things do appear to fail.

Unfortunately, the solution you've proposed is impossible for me. More often than not the values I need to replace are JSON objects themselves (I can't very well define a variable that's named like a JSON object), locked in a complex hierarchy of other JSON objects. Though I've decided to abandon this design in favor of another one anyway.

Post Reply

Return to “Documentation Requests/Discussion”