[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Use of '()' in a regexp
From: |
Ed Morton |
Subject: |
Use of '()' in a regexp |
Date: |
Tue, 5 Jan 2021 12:38:58 -0600 |
User-agent: |
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 |
Someone just pointed this out to me (gawk 5.1.0):
$ printf 'foo\n' | awk '{gsub(/()/,"x")} 1'
xfxoxox
$ printf 'foo\n' | awk -v RS='()' -v ORS='x\n' '1'
foox
Obviously that's a pretty ridiculous regexp but it still has me
wondering - why does `gsub()` treat the regexp `()` as matching a null
string around every character while `RS` treats it as if I'd asked it to
match the `\n` at the end of the input:
$ printf 'foo\n' | awk -v RS='\n$' -v ORS='x\n' '1'
foox
I could just file this under "don't write stupid regexps" but I was
wondering if there's a more concrete, satisfying explanation of the
behavior.
Ed.