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
932 views
in Technique[技术] by (71.8m points)

regex - sed error "Invalid range end"

I run this substitution command on Ubuntu 12.04.

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

However, the following error is raised.

sed: -e expression #1, char 27: Invalid range end

I can remember the same expression works on MacOSX.
Can you describe why the command fails?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You can solve it in two ways. One of them is to use -r switch to avoid escaping special characters and move - in the range to first or last position and avoid its special meaning, it would be like:

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

Otherwise you will need to escape either (, ) and +, like:

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

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

2.1m questions

2.1m answers

60 comments

56.9k users

...