AI Zone Admin Forum Add your forum

NEWS: survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

^substitute or ^reviseOutput between two characters

I would like to be able to strip output between two characters. If this was regex, I would do something like \((.*?)\) to strip out all content between (and including) two parentheses.

I can see that ^substitute and ^reviseOutput allow me to replace specific characters or words with alternatives, but I can’t quite see from the manuals how I would say, replace everything between x and y.  Am I missing something obvious, or not obvious, or is there no way to do this in CS? I guess one workaround would be to create a JavaScript macro, but I rather do this natively if possible. It seems like pretty standard text processing stuff so I’m hoping I may have missed it.


  [ # 1 ]

use ^extract before and ^extract after and ^join


  [ # 2 ]

Thanks as ever for the lightning response. I thought there would be a native way, but I don’t think I’d have come up with that quickly on my own. Will check it out.


  [ # 3 ]

Ha. I’m trying to extract everything between 2 parentheses - as in ^extract(@0object ( \)) but it seems I can’t quite get to the closing parenthesis. If I don’t escape the first one, it thinks the second parenthesis marks the expression’s end and is missing a parameter. If I escape the first one it thinks the others are still escaped and it’s not finding a closing parenthesis. Is this a bug or something I’m missing?

Errorline 443 of
CALL-59 Failed to properly close (or [ in call to ^extract 


*** Errorline 440 of
in at 440:   ^extract(@0object ( ))

CALL-60 Incorrect argument count to system function ^extractgiven 2 instead of required 3 



  [ # 4 ]

I see I misunderstood your original answer, although I’m still hitting the escaping issue. To strip the pronunciation guides or dates in wikipedia I am trying the two extracts and joins, but the parentheses are proving elusive:

?: (what be {a} _*~>) ^keep() $$tmp '_0
 $$url = ^"\";=&explaintext;=&format=json&utf8;=&redirects;=&titles;=$$tmp\""
 # Url escaping is done by ^jsonopen
 $$user_agent = ^" User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"
 $$tmp = ^jsonopen(GET $$url "" $$user_agent)
  # ^jsontree($$tmp)
  # ^jsonpath(  ".query .pages .56333 .extract" $$tmp)
  @0 = ^query(direct_v ? extract ? 1)
  $part1 = ^extract(@0object ? ()
  $part2 = ^extract(@0object /) ? )
  $answer = ^join($part1 $part2)

  [I believe] [My guess would be] [I sense] [It is probable that] $answer 

Is that the approach you meant? I am still getting the escaping errors reported in my answer above. Thanks in advance for any guidance.


  [ # 5 ]

Extract takes as argument the index in the word (start and end). Since you dont know the index, you need to use ^find to get it


  [ # 6 ]

Thanks both for your input on this.  I was struggling as well.

the following is working for me:

$short_summary = ^next(FACT @3object)
$paren1 = ^findtext$short_summary \) - 1
= ^findtext$short_summary \)
found parens $paren1 $paren2
= ^extract$short_summary 0 $paren1 )
$part2 = ^extract$short_summary $paren2 ^length($short_summary) )
$paren1 = ^findtext$part2 \)
$paren2 = ^findtext$part2 \)
 if ( 
$paren2 $paren1 {
= ^extract$part2 $paren2 ^length$part2 ) )
= ^join$part1 $part2 )
english summary$short_summary_clip 

The second set of ^findtext followed by the if block catches the case I have where there are nested brackets but it would fail if there is just one set of brackets in the whole starting text, so not yet sound coding.

Note that you have a forward slash in your code above / but you need a backslash to find the brackets: \( \)


  [ # 7 ]

Thanks a lot for this. I was a bit hurried in the end for a demo, so I cheated and post-processed the CS output on the front end with a bit of regex

CSoutput = response.replace(/\((.*?)\)/, “”);

I will now try your helpful code - super clarifying. Thank you both.


  login or register to react