Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for non-raw strings as a delimiter option (doubly-escaped symbols) #2223

Open
seojumper opened this issue Feb 6, 2024 · 4 comments

Comments

@seojumper
Copy link

seojumper commented Feb 6, 2024

Feature

We use the site for helping to build out regex patterns for Robot Framework which provides a number of keywords for using regex patterns in tests. These keywords require regex patterns provided to keywords be "double escaped" as a lone '\' char will cause the script to fail to be parsed correctly by the Robot runtime.

From Robot's documentation https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Regular%20expressions

Regular expressions

Some keywords, for example Should Match Regexp, support regular expressions that are more powerful but also more complicated that glob patterns.
The regular expression support is implemented using Python's re module and its documentation should be consulted for more information about the syntax.
Because the backslash character (\) is an escape character in Robot Framework test data, possible backslash characters in regular expressions need to be escaped with another backslash like \\d\\w+.
Strings that may contain special characters but should be handled as literal strings, can be escaped with the Regexp Escape keyword.

Inside of a robot script file an example usage of one of such keywords must look like this where you see the pattern must have double escaped symbols or will not be parsed correctly.

${interface_status}=    Get Regexp Matches
...    ${interface_output}
...    (\\S+)\\sis (up|down|administratively down), line protocol is (up|down)

Since we are needing to work "in code" with "doubly escaped" symbols in patterns, it would be handy if the site could offer a non-raw delimiter option for its python engine that understands doubly-escaped symbols.

Currently when writing these files (as a number of folks enjoy using the site to aid in creating/tshooting the patterns they write) we are taking a doubly-escaped pattern from Robot script > copying it over to the site > manually removing double escapes so the site can understand it > fixing the pattern with the help of the site >
copying the fixed pattern back into robot and manually re-inserting the double escape sequences where needed which is a very error prone process, and also a friction point when you need to make multiple iterations on a pattern.

Would be nice if support for this could be reconsidered as it would certainly make the site that much easier to use for our (and other Robot users) use case.

@working-name
Copy link
Collaborator

Hello @seojumper,

Here's some potentially helpful things in the meantime: Replace \\ with \

To get the escaped stuff, select javascript as the code generator syntax. No flags, but it seems they're limited and can be set inline as per the linked documentation.

@seojumper
Copy link
Author

seojumper commented Feb 6, 2024

That is a handy workaround/workflow that I'll add to our teams docs. Thank you.

@zhangt2333
Copy link

zhangt2333 commented Jul 14, 2024

+1

I need to manually replace \\ with \ every time I paste Regex from my Java code to Regex101.

@DanKaplanSES
Copy link

DanKaplanSES commented Dec 16, 2024

This comes up all the time in JavaScript, too.

Sometimes you need to define a regex as a quoted string, e.g., if you want to use a dynamic variable in the pattern (new RegExp(`foo${dynamicVar}bar`)). In this scenario, you must escape \'s with \\. And if you are trying to represent a literal \ character, you must type \\\\.

From a usability standpoint, I often confuse myself by going back and forth between these formats.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants