Search code examples
regexcalculated-columnsspotfiretibco

RXReplace Function Gives 'Invalid escape sequence: "\B".' #Error


I'm using TIBCO Spotfire Analyst 7.8.0 HF-007 and trying to add a calculated column to create abbreviations for a string using a regular expression in the RXReplace function. However, I get an error stating "Invalid escape sequence: "\B"." which seems to not recognize the not word boundary.

The function does recognize the word boundary "\b". I've searched for an alternative to the not word boundary ("\B") expression, but have not found one.

The function call looks like: RXReplace([Hospital_Name],"\B[a-zA-Z'-]+","","g")

Is there an alternative to "\B" or a different approach to abbreviate a phrase (e.g., "MY HOSPITAL'S NAME" > "MHN")?


Solution

  • You may use

    RXReplace([Hospital_Name], "\\B[a-zA-Z'-]+|\\W+", "", "g")
    

    See the regex demo.

    The backslashes should be doubled (to define one backslash \\ must be written inside the string literal) and to remove any other non-word chars you need a \W+ alternative in your regex.

    See the RXReplace documentation:

    Some characters, like for instance the backslash character "\", need to be escaped to work when using calculated columns.

    and

    The backslash needs to be escaped twice; once for the Spotfire string and once for the regular expression.