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

Add feature for formatting and indenting regexes on multiple lines #17

Closed
pemistahl opened this issue Feb 19, 2020 · 2 comments
Closed

Comments

@pemistahl
Copy link
Owner

pemistahl commented Feb 19, 2020

Generated regular expressions which are very long are often hard to read. It would be useful to have an option to indent and format the generated regexes on multiple lines to make them more readable.

@pemistahl pemistahl added the enhancement New feature or request label Feb 19, 2020
@pemistahl pemistahl added this to the grex 1.1.0 milestone Feb 19, 2020
@pemistahl pemistahl modified the milestones: grex 1.1.0, grex 1.2.0 Apr 3, 2020
@skington
Copy link

skington commented Oct 29, 2020

I came looking for an issue like this and was pleased that it had already been raised. IMO, anything with more than a few moving parts should automatically have the /x switch.

The corollary, though, is that if the generated regexp would contain large sequences of text including spaces, and turning on the /x flag would replace all the spaces with escaped spaces, then said large sequences of text should instead be quoted with \Q and \E.

As it is, if you take a handful of memorable phrases from e.g. the Declaration of Independence, you get the following:

Grudgingly:~ sam$ grex 'We hold these truths to be self-evident' 'that all men are created equal' 'that they are endowed by their Creator with certain unalienable Rights' 'that among these are Life, Liberty and the pursuit of Happiness.'
^(?:that (?:they are endowed by their Creator with certain unalienable Rights|a(?:mong these are Life, Liberty and the pursuit of Happiness.|ll men are created equal))|We hold these truths to be self-evident)$

(I don't know why the hyphen is escaped there, incidentally.)

As a human, I'd write the resulting regexp something like this:

^
    (?: that \s
        (?:
            \Qthey are endowed by their Creator with certain unalienable Rights\E
        |
            a (?:
                \Qmong these are Life, Liberty and the pursuit of Happiness.\E
              | \Qll men are created equal\E
            )
        )
    |
    \QWe hold these truths to be self-evident\E
    )
$

@pemistahl
Copy link
Owner Author

Hi @skington, thanks for your input. I haven't found the time so far to continue the development of grex but as soon as I find the time, I will consider your suggestions.

Feel free to send me a pull request if you are not afraid of the code. ;)

pemistahl added a commit that referenced this issue Dec 18, 2020
pemistahl added a commit that referenced this issue Jan 2, 2021
pemistahl added a commit that referenced this issue Jan 2, 2021
@pemistahl pemistahl added new feature and removed enhancement New feature or request labels Jan 2, 2021
pemistahl added a commit that referenced this issue Jan 2, 2021
pemistahl added a commit that referenced this issue Feb 26, 2021
pemistahl added a commit that referenced this issue Mar 8, 2021
pemistahl added a commit that referenced this issue Mar 10, 2021
pemistahl added a commit that referenced this issue Mar 28, 2021
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

2 participants