codingenious
codingenious

Reputation: 8653

Regex - script check for allowed characters

I need to check a string and make sure that it does not contain any character outside list of given characters.

I wrote one script to test this:

str="$1"

regex="^[0-9a-zA-Z\,\!\-\^\`@\{\}\[\],=\(\).;\/~_|]*$"

if [[ "$str" =~ $regex ]]
then
  echo "f yeah"
else
  echo "f you"
fi

But it does not seem to work. And it seems to me that I am not even close.

List of allowed characters is :

a-z A-Z 0-9 ` @ { } [ ] ^ , = ! ( ) . ; / ~ _ |

Not sure what I am missing here.

Upvotes: 1

Views: 572

Answers (2)

ghoti
ghoti

Reputation: 46856

As Chepner indicated, most "special" characters lose their specialness if you put them inside a range (square brackets). Also, per man re_format:

To include a literal ] in the list, make it the first character (following a possible ^). To include a literal -, make it the first or last character, or the second endpoint of a range.

If we re-order your range to match these rules, I would expect the following to work:

regex='^[][a-zA-Z0-9`@{}^,=!().;/~_|]*$'

And if you also want a hyphen:

regex='^[][a-zA-Z0-9`@{}^,=!().;/~_|-]*$'

Upvotes: 2

chepner
chepner

Reputation: 531848

In general, characters have no special meaning inside [ ... ] in a regular expression. You need to quote a few characters to prevent bash from treating them specially while defining the variable that holds the regular expression, but that is most easily done by single-quoting the string.

regex='^[0-9a-zA-Z,!\-^`@{}[\]=().;/~_|]*$'

Only the hyphen (if it is not the first or last character in the bracket expression) and the ] (if it is not the first character) need to be escaped. For example:

regex='^[-0-9a-zA-Z,!^`@{}[\]=().;/~_|]*$'
regex='^[]0-9a-zA-Z,!\-^`@{}[=().;/~_|]*$'

are both valid.

Upvotes: 2

Related Questions