Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@nanbuwks

角括弧[]を正規表現で置換

More than 1 year has passed since last update.

角括弧を置換したい


[abc]


"abc"

にしたい。

エスケープすればいいよね?


$ echo [abc] | sed 's/[\[\]]/\"/g' 
[abc]

おや。

正解

こちらによると、


$ echo [abc] | sed 's/[][]/\"/g' 
"abc"

できた。

理由

はこちら
https://www.gnu.org/software/sed/manual/sed.html#Character-Classes-and-Bracket-Expressions
「sed, a stream editor」

「5.5 Character Classes and Bracket Expressions」
によると、

‘]’
ends the bracket expression if it’s not the first list item. So, if you want to make the ‘]’ character a list item, you must put it first.

ということで、] は最初に配置する必要があるらしい。

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
nanbuwks
iotlt
IoT縛りの勉強会です。 毎月イベントを実施しているので是非遊びに来てください! 登壇者を中心にQiitaでも情報発信していきます。 https://iotlt.connpass.com

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?