Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using brackets inside a character class in a regular expression

Tags:

bash

sed

I have a string:

['A', 'B']

I need to remove all ,, [, and ] characters. The final result should be

'A' 'B'

Below is a list of everything I tried, and the results

Commands I tried

user@vm:~$ echo "['A', 'B']" | sed -r 's/[\[\],]//g'
['A', 'B']

user@vm:~$ echo "['A', 'B']" | sed -r 's/[[],]//g'  # unescaped
['A', 'B']

user@vm:~$ echo "['A', 'B']" | sed -r 's/[\[\]]//g'  # removed ","
['A', 'B']

user@vm:~$ echo "['A', 'B']" | sed -r 's/[,]//g'  #removed "[" and "]"
['A' 'B']

user@vm:~$ echo "['A', 'B']" | sed -r 's/[[,]//g'  # removed "]"
'A' 'B']

Obviously, none of them worked. However, these commands did:

user@vm:~$ echo "['A', 'B']" | sed -r 's/[],[]//g'
'A' 'B'

user@vm:~$ echo "['A', 'B']" | sed -r 's/[][,]//g'
'A' 'B'

Why did this work? Differences between commands above and below:

  • The [, ] are not escaped
  • The order is different (] before [)

Why does the order matter?

like image 646
Sagar Avatar asked May 04 '26 06:05

Sagar


1 Answers

From info sed (see also https://www.gnu.org/software/sed/manual/sed.html#Character-Classes-and-Bracket-Expressions)

A leading '^' reverses the meaning of LIST, so that it matches any single character not in LIST. To include ']' in the list, make it the first character (after the '^' if needed), to include '-' in the list, make it the first or last; to include '^' put it after the first character.

And as mentioned in comments, tr is better suited

$ echo "['A', 'B']" | tr -d '[],'
'A' 'B'
like image 54
Sundeep Avatar answered May 05 '26 23:05

Sundeep