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

In regex_replace action, consider doing line-by-line matching instead of entire file #120

Open
drevell opened this issue Aug 2, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@drevell
Copy link
Contributor

drevell commented Aug 2, 2023

TL;DR

As a template developer, I want ^ and $ to match beginning and end of line, because that's what I'm used to from grep. As a template developer I'm probably not aware of how to do RE2 multiline regexes to manually enable this behavior using (?m:my_regex).

cc @sethvargo @pdewilde @verbanicm who have been involved in related conversations.

Upside:

  • Users don't have to understand RE2 multiline syntax and semantics to match beginning/end of line
    Downside:
  • If users want to match a string that spans multiple lines within a file, we have to give them a special mode.

Implementation ideas:

  • split each input file on \n before doing the regex replace
  • add a new mode for users that want to do a search-and-replace that spans multiple lines. Something like:
    action: `regex_replace`
    params:
      paths: ['my.txt']
      replacements:
        mode: 'file_at_a_time'  # <---- here we need a new mode
        regex: |
          my
          regex
          with
          newlines
        replace_with: 'whatever'
    

Detailed design

No response

Alternatives considered

No response

Additional information

No response

@drevell drevell added the enhancement New feature or request label Aug 2, 2023
@pdewilde
Copy link
Contributor

pdewilde commented Aug 2, 2023

split each input file on \n before doing the regex replace

This would make it more grep-like, though it would have different semantics than for example my text editor, where $ and ^ are done per-line, but I can still replace \n if I need to.

I was thinking just having users use \n instead of $ or ^, but unfortunately that breaks on the first/last line of a file, where there is no preceding/trailing \n.

@drevell
Copy link
Contributor Author

drevell commented Aug 3, 2023

This would make it more grep-like, though it would have different semantics than for example my text editor, where $ and ^ are done per-line, but I can still replace \n if I need to.

@pdewilde, could it be that your editor is in multiline mode by default? It doesn't look like we have the ability to make that happen in Go 😞 . Unless we resort to using https://pkg.go.dev/regexp/syntax#Prog which is a lower-level API to regexes that looks like dragons. But at first glance it does seem desirable to default to multiline mode.

We could wrap the user's regex in (?m:their_regex), but that seems gross. Maybe acceptably gross though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants