proposal: support the `x` flag
- Dominant language
- C++
- Stars
- 9.8k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
Originally posted https://github.com/golang/go/issues/79835, but was informed I should post this here.
The `x` flag instructs the parser to ignore most whitespace, unless it is backslashed or within brackets. It's useful for breaking up long patterns into readable parts. Additionally, the `x` flag allows comments within the pattern, starting with a `#`
For example:
```perl
# multiline.pl
s/
# A comment
lang
|
pher
/!/x
```
```shell
echo "Golang" | perl -p multiline.pl # Go!
echo "Gopher" | perl -p multiline.pl # Go!
```
More details: https://perldoc.perl.org/perlre#/x-and-/xx
---
I haven't found any duplicate issues, but I did find https://github.com/google/re2/issues/3#issuecomment-91317250, which says that `x` is arguably unnecessary.
There are certainly workarounds for breaking up a regular expression into multiple lines *without* the `x` flag. In Go terms, one could use `strings.Join` on an array of strings, for example, and it would also be pretty simple to clean up the strings to remove comments and ignored spaces.
In my personal use case, when a regular expression gets significantly complicated, I like to move it to its own file, and use `go:embed` (or the equivalent in another language). This helps avoid clutter in the code file IMO, and allows one to focus on the code logic. I believe that the `x` flag helps support this use case.
To my knowledge, `x` is currently supported in Perl, Ruby, and Rust.
Thanks!
Contributor guide
Assessment
This issue has not been assessed yet.