[conspire] Basic Regular Expressions (GNU/Linux "vs." BSD)

Michael Paoli Michael.Paoli at cal.berkeley.edu
Thu Feb 18 01:17:27 PST 2021


Ah, drats ... I'd expect I ought get the same from both of these, ...
but no.  Not quite sure if I'm not interpreting things exactly right,
or there's sufficient wiggle room with BRE on this in POSIX, or there
might possibly actually be a bug in the BSD case on this.
But, with both grep and sed, seeing quite different results on these,
notably BRE not matching, as in fact expected, yet on BSD matching
where not expecting it to match:

GNU/Linux:
$ echo 'xxo o x  ' | sed -ne '/^\(...\)\{0,2\}\([ox]\)\2\2/{s/.*/matched/p;}'
$ echo 'xxo o x  ' | grep -e '^\(...\)\{0,2\}\([ox]\)\2\2'
$ echo 'xxo o x  ' | sed -ne 's/^\(...\)\{0,2\}\([ox]\)\2\2/1:\12:\2\&:&/p'
$

BSD:
$ echo 'xxo o x  ' | sed -ne '/^\(...\)\{0,2\}\([ox]\)\2\2/{s/.*/matched/p;}'
matched
$ echo 'xxo o x  ' | grep -e '^\(...\)\{0,2\}\([ox]\)\2\2'
xxo o x
$ echo 'xxo o x  ' | sed -ne 's/^\(...\)\{0,2\}\([ox]\)\2\2/1:\12:\2\&:&/p'
1:xxo2:&:xxo o x
$

How can \([xo]\) matching zero characters be correct?
That seems broken to me.

But these work the same on both, as expected:
$ echo 'xox' | grep '^\([xo]\)\1\1$'
$ echo 'xxx' | grep '^\([xo]\)\1\1$'
xxx
$ echo 'xox' | sed -ne '/^\([xo]\)\1\1$/p'
$ echo 'xxx' | sed -ne '/^\([xo]\)\1\1$/p'
xxx
$




More information about the conspire mailing list