Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

regex - sed error: "invalid reference 1 on `s' command's RHS"

I run several substitution commands as the core of a colorize script for maven. One of the sed commands uses a regular expression which works find in the shell as discussed here. The current (not working) implementation can be found here.

When I include one of the variants of the command into the script different behavior occurs:

Variant 1:

$ sed -re "s/([a-zA-Z0-9./\ :-]+)/1/g"

Adapted to the script:

-re "s/WARNING: ([a-zA-Z0-9./\ :-]+)/${warn}WARNING: 1${c_end}/g" 

Error: The shell outputs the same information as if I would type $ sed. Strange!?


Variant 2:

$ sed -e "s/([a-zA-Z0-9./\ :-]+)/1/g"

Adapted to the script:

-e "s/WARNING: ([a-zA-Z0-9./\ :-]+)/${warn}WARNING: 1${c_end}/g" 

Error:

sed: -e expression #7, char 59: invalid reference 1 on `s' command's RHS

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Don't you need to actually capture for that to work? i.e. for variant #2:

-r -e "s/WARNING: (([a-zA-Z0-9./\ :-]+))/${warn}WARNING: 1${c_end}/g" 

(Note: untested)

Without the -r argument back-references (like 1) won't work unless each parenthesis is escaped with a character.

With -r, argument back-references (like 1) won't work unless the parenthesis are NOT escaped.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...