You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] sed treats ( ) { } + ? | as regex operators in basic regular expressions #181
I have updated to the latest minor and patch version of Strands Shell
I have checked the documentation and this is not expected behavior
I have searched ./issues and there are no duplicates of my issue
Binding
Python
Strands Shell Version
0.5.0
Language Runtime Version
Python 3.12.3
Operating System
Ubuntu 24.04.5 LTS (x86_64)
Installation Method
pip (strands-shell)
Steps to Reproduce
importstrands_shellshell=strands_shell.Shell()
forcmdin [
"echo 'call(x)' | sed 's/call(x)/run(x)/'",
"echo 'a|b' | sed 's/a|b/X/'",
"printf 'f(x)\\n' | sed -n '/f(x)/p'",
"echo 'f(x)' | sed -E 's/f\\(x\\)/g/'",
]:
print(repr(shell.run(cmd).stdout))
Expected Behavior
sed patterns are POSIX basic regular expressions, so a bare (, ), {, }, +, ? and | matches itself, and only \(, \), \{, \} (and the GNU \+, \?, \|) are operators. With -E, the pattern is an extended regular expression. GNU sed 4.9 prints:
'run(x)\n'
'X\n'
'f(x)\n'
'g\n'
Actual Behavior
'call(x)\n'
'X|b\n'
''
'f(x)\n'
The bare characters act as regex operators, so s/call(x)/run(x)/ changes nothing, s/a|b/X/ replaces only the a, and the address /f(x)/ matches nothing. The usual result is an edit that silently does nothing: sed -i 's/open(path)/open(path, encoding="utf-8")/' file.py leaves the file unchanged and exits 0.
Additional Context
bre_to_ere in src/commands/sed.rs turns \(, \), \{ and \} into operators but passes the bare characters through unchanged, and the regex crate reads them as operators too. Address patterns are not converted at all, and -E and -r fall into the _ => {} option arm and are ignored, so sed -E gets the same conversion. sed.rs is the same on main (0b2c8c0) as in v0.5.0.
Possible Solution
Escape the bare characters in bre_to_ere, map \+, \? and \|, keep a leading * and a $ that is not at the end literal, convert address patterns too, and honour -E, -r and --regexp-extended. This changes behavior for scripts that use ERE syntax without -E: sed 's/a+/X/' would then treat + literally, as GNU sed does, while sed -E 's/a+/X/' keeps working.
I have a fix with tests and will open a PR if this approach works for you.
Activity