Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string contains non digit characters

Tags:

grep

bash

How can I check if a given string contains non numeric characters, examples :

x11z returns > 0
x$1 also returns > 0
1111~ also returns > 0

By character I mean everything not between 0-9. I saw similar threads but non of them talks about "non 0-9" except they show if its a-z or A-Z.

like image 970
saeed hardan Avatar asked Jun 01 '13 17:06

saeed hardan


2 Answers

Just use a negated character class:

grep [^0-9]

This will match any non-numeric character, and not strings composed of only digits.

like image 106
squiguy Avatar answered Sep 23 '22 00:09

squiguy


Just by using bash pattern matching:

[[ "$MY_VAR" =~ ^[^0-9]+$ ]] && echo "no digit in $MY_VAR"
like image 27
Salah Eddine Taouririt Avatar answered Sep 23 '22 00:09

Salah Eddine Taouririt