Search code examples
regexregex-lookaroundsnegative-lookbehindhpcc-ecl

HPCC ECL REGEX lookaround back-to-back values


I am using the REGEX Library as defined here http://userguide.icu-project.org/strings/regexp

That being said I have a specific problem I would like to use regex to correct.

Input := '!Tim !~ Dan~'; 
Output := Tim !~ Dan

REGEXREPLACE('((?<![!])~)' ,Input,' '); //Results in : !Tim !~ Dan
REGEXREPLACE('((?![~])!)|((?<![!])~)',Input,' '); //Results in : Tim ~ Dan

What about this statement is causing the ! following the ~ to be removed?

How do I remove all ! and ~ without removing the combination of !~ in a single Regex command.

BONUS POINTS if you can tell me how to make !~! work in a single command.

Thanks!!


Solution

  • You can use the following regex:

    (!~)|[!~]
    

    and replace with $1 backreference. See the regex demo.

    Details:

    • (!~) - matches and captures into Group 1 a !~ sequence of characters
    • | - or
    • [!~] - a single ! or ~ character

    The $1 backreference re-inserts the value stored in Group 1 back to the resulting string.