LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Replace

    Employee
    Posted 01-12-2010 05:08

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: michaelslowey

    Is there any way to replace multiple exact occurences of a word within a string.

    For example

    I want to replace "NO" here with "YES"

    emit "NO I AM GOING NORTH".replace("NO","YES") as text

    The emitted out put becomes "YES I AM GOING YESRTH"

    I could use...

    emit "NO I AM GOING NORTH".replace("NO ","YES ") as text

    But that would also replace "NO" at the end of words (eg "BONO") and not replace "NO" at the end of a piece of text (eg "THIS SIGN SAYS NO")


  • 2.  RE: Replace

    Employee
    Posted 01-12-2010 05:36

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ddonovan

    You need to use the regular expression operators to handle more complex situations such as this. The below example will accomplish what you want, using 3 regexSubstitute() calls. There is probably a way to consolidate these into a single regular expression, but I will leave that as an exercise for the reader.

     
    emit text.regexSubstitute("NO\\w+", "YES").regexSubstitute("\\w+NO\\w+", "YES").regexSubstitute("^NO", "YES") as newText


  • 3.  RE: Replace

    Employee
    Posted 01-12-2010 05:55

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: michaelslowey

    When I use those commands with this text "NO IAM AM NOT GOING NORTH" I get "YES IAM AM YES GOING YES" as the output.

    the help doesn't have the extended instructions I need.

    cheers
    mick


  • 4.  RE: Replace

    Employee
    Posted 01-12-2010 06:00

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ddonovan

    Yeah, the regular expressions I had in my example were not completely correct for the rule that you want. It was only intended as an example to handle the specific case that you mentioned before. Regular expressions within BrainScript follow the Perl RegEx syntax. There are many tutorials for this available online. Here is one that includes some examples:

    http://www.troubleshooters.com/codec...rl/perlreg.htm

    Others are also available which go into much more detail. Repost here if you have trouble figuring it out, and I will be happy to provide some more assistance.

    Cheers.


  • 5.  RE: Replace

    Employee
    Posted 01-12-2010 06:21

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: michaelslowey

    Problem Solved...

    text = "NO I AM AM NOT GOING NORTH NO"

    emit text.regexSubstitute("NO\\b", "YES") as text

    The "switches" on the regexSubstitute supply the answer... in this case "\\b" only replaces at a word border "NO...." , "...NO..." or "...NO"

    This gives "YES I AM AM NOT GOING NORTH YES"

    Cheers
    Mick