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

A way to store named group(s) in the UI so it can be used via subroutine calls in a new regular expression? #2316

Open
A-Paint-Brush opened this issue Aug 2, 2024 · 0 comments

Comments

@A-Paint-Brush
Copy link

Feature

If you've already finished creating a long regex, and now you want to write another regex that reuses the first one in several places, then it seems the only way is to copy-paste the first regex to the first location in the second regex where you want to use it, and then use the "subroutine" syntax ((?N) or (?&name)) in the remaining locations to refer to it. This makes it really hard to write the second regex, because the first one already takes up several lines of the text box and adds a lot of visual complexity.

When using regexes in a programming language, this can be solved by storing the first regex in a separate string variable, and then writing the second regex as a formatted string and formatting the first regex into it. Like in Python for example, if you define the first regex as expr1 = r"^really really long regex$", then you can write your second regex as expr2 = r"...({g1})...(?1)...".format(g1=expr1[1:-1]) to avoid having to copy paste the first regex (like expr2 = r"...(really really long regex)...(?1)...").

Therefore, it would be nice if it is possible to do something similiar in Regex101. Like it can allow you to store a list of named groups in the UI (maybe a button like "stored named groups" that brings up a modal when clicked, which displays all currently stored groups and an "add entry" button at the bottom), which would allow you to enter a regex and the name to store it under. And then in new regex, you can refer to it by its name using the (?&name) syntax without having to copy and paste it anywhere into the new one.

To give a more concrete example, I just wrote a (hopefully bug-less) regex that matches string literals:
^(?P<str_lit>(?P<begin_quote>(?P<single_quote>')|\")(?:(?(single_quote)\"|')|\\[\"']|\\\\|[^\"'\\\r\n])*?(?P=begin_quote))$
, and now I'm working on writing a second regex that would match an entire expression in a C-like language. And since an expression obviously can contain string literals, I have to copy-paste the entire regex into the first reference to it in the second regex before I can use (?&str_lit) in further references, which is making it a lot harder for me to work on the second regex because of all the added visual complexity (I'm terrible at regexes). If this feature existed, then I can just store (?P<begin_quote>(?P<single_quote>')|\")(?:(?(single_quote)\"|')|\\[\"']|\\\\|[^\"'\\\r\n])*?(?P=begin_quote) under the name str_lit in the UI. Then in the new regex, I can start with a blank canvas (well, text box) and use (?&str_lit) when I need to match a string literal.

@A-Paint-Brush A-Paint-Brush changed the title A way to store named group(s) in the UI so it can be used as subroutine calls in the regular expression? A way to store named group(s) in the UI so it can be used via subroutine calls in a new regular expression? Aug 2, 2024
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

1 participant