zyedidia / sregx

A tool and library for using structural regular expressions.
MIT License
59 stars 4 forks source link

Suggestion: Capture groups? #2

Closed Mango0x45 closed 2 years ago

Mango0x45 commented 2 years ago

Adding support for capture groups would make certain tasks much easier. For example, what if I wanted to duplicate every character? With regular sed this is as easy as s/./&&/g, or more explicitly s/\(.\)/\1\1/g. I have not found a way to achieve this with sregx yet though (without the use of /u/ that is).

zyedidia commented 2 years ago

This is in fact supported via Go's regular expression syntax. You can use s/(.)/$1$1/ for example. You can read about the regex syntax here: https://pkg.go.dev/regexp/syntax, and about the expansion syntax here: https://pkg.go.dev/regexp#Regexp.Expand.

Mango0x45 commented 2 years ago

Ah that makes a lot of sense. Thanks!

demostanis commented 9 months ago

is there a way to just print the capture group? something like 'g/hello ([a-z]+)/c/$1/' which would print world when inputted hello world PS: why doesn't \w work?