Ask Reuben

Back Quote

Why does my regular expression not work? 

How do I stop \ having special meaning in my text literal?

Regular expressions were added in Genero Enterprise 4.00. No longer do you need to use IMPORT JAVA in order to code regular expressions.  You can use the new methods introduced in 4.00 as part of the util.Regexp class as well as the additional methods added to the STRING data-type.

During the Early Access Program, something became very apparent. If you copied a regular expression directly from a website such as Stack Overflow or the many sites dedicated to regular expressions such as https://regexr.com/ , you would not get the expected matches. This was because of two contrasting properties…

  1. The back slash \ character is used in the syntax for pattern matching in regular expressions. For example \d would match any digit, \b would match any whitespace. A full list is given here
  2. The back slash character has special meaning in text literals (Note: link to 3.20 documentation).  For example \n was new line, \" and \' allowed you to use " and ' inside text literals e.g. 'O\'Sullivan', "WHERE col=\"A\"" . To have a genuine \ character in a quote you needed to express it as \\. DISPLAY "foo\\bar" would appear as foo\bar

What was happening with our new regular expressions, you might copy and paste code "^\d\d$" but the Genero text literal rules would see that passed as "^dd$" to the regular expression method, and you would not get the result intended.  Strictly speaking what you needed to do was prefix any back slash \ with another back slash.  For example "^\d\d$" needed to become "^\\d\\d$"

That situation wasn’t ideal and so a better solution was worked out. That involved the introduction of a third text delimiter for use with text literals (Note : link is to current version documentation).  That third text delimiter was the back quote ` character.  We also gave it the property that the back slash \ did not have any special meaning with this new quote delimiter. For example DISPLAY `Line One\nLine Two` will display as …

Line One\nLine Two

not as 

Line One

Line Two

When using regular expressions, you should get into the habit of using this new back quote delimiter.  When copying and pasting a regular expression, use the back quote character ` to delimit the text literal, NOT the single or double quote character.

It is a lot easier to change the 'or " to a `, then it is to trawl through a regular expression such as [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])? and change all \ to \\  !!!