16

Trying to verify that a string has only lowercase, uppercase, or numbers in it.

if ! [[ "$TITLE" =~ ^[a-zA-Z0-9]+$ ]]; then echo "INVALID"; fi

Thoughts?

* UPDATE *

The variable TITLE currently only has upper case text so it should pass and nothing should be outputted. If however I add a special character to TITLE, the IF statement should catch it and echo INVALID. Currently it does not work. It always echos invalid. I think this is because my regex statement is wrong. I think the way I have it written, its looking for a title that has all three in it.

Bash 4.2.25

The idea is, the user should be able to add any title as long as it only contains uppercase, lowercase or numbers. All other characters should fail.

* UPDATE *

If TITLE = ThisIsAValidTitle it echos invalid.

If TITLE = ThisIs@@@@@@@InvalidTitle it also echos invalid.

* SOLUTION *

Weird, well it started working when I simplified it down to this:

TEST="Valid0"
if ! [[ "$TEST" =~ [^a-zA-Z0-9] ]]; then
  echo "VALID"
else
  echo "INVALID"
fi

* REAL SOLUTION *

My variable had spaces in it... DUH

Sorry for the trouble guys...

* FINAL SOLUTION *

This accounts for spaces in titles

if ! [[ "$TITLE" =~ [^a-zA-Z0-9\ ] ]]; then
  echo "VALID"
else
  echo "INVALID"
fi
5
  • 1
    What was the result? What were you expecting? Commented Aug 4, 2013 at 10:02
  • The variable TITLE currently only has upper case text so it should pass and nothing should be outputted. If however I add a special character to TITLE, the IF statement should catch it and echo INVALID. Currently it does not work. It always echos invalid. I think this is because my regex statement is wrong. I think the way I have it written, its looking for a title that has all three in it. Commented Aug 4, 2013 at 10:04
  • Seems to work fine for me. What version of bash are you using? Commented Aug 4, 2013 at 10:05
  • @Atomiklan, you should add that to the question. Commented Aug 4, 2013 at 10:06
  • How and what are you writing to TITLE? Commented Aug 4, 2013 at 10:11

3 Answers 3

22

I'd invert the logic. Test for invalid characters and echo a warning if at least one is present:

if [[ "$TITLE" =~ [^a-zA-Z0-9] ]]; then
  echo "INVALID"
fi

With that said, your original check worked for me, so you probably need to provide more context (i.e. a larger portion of your script).

Sign up to request clarification or add additional context in comments.

5 Comments

No change. If TITLE = ThisIsAValidTitle it echos invalid. If TITLE = ThisIs@@@@@@@InvalidTitle it also echos invalid.
Which shell are you running this in (i.e. what is the shebang line of your script)?
I give you partial credit as you did help me change the syntax around. Thank you
What if $TITLE is empty?
@dgorissen Then the code from my answer would do nothing, obviously. If you wanted that covered as well you could do something like ... =~ [^a-zA-Z0-9]|^$.
2

why cant we use alnum

[[ 'mystring123' =~ [:alnum:] ]] && echo "ok" || echo "no"

2 Comments

"why can't we use alnum" is a question, not an answer. Consider though [[:alnum:]] or [![:alnum:]] as part of a possible answer.
Try e.g. STR="mystring123"; test -z "${STR##*[[:alnum:]]*}" && echo yay || echo nay - this even works with dash/ POSIX.
1

the nominated answer is wrong. Because it doesn't check to the end of the string. also it's inverted. as the conditional says: "if the start of the string is valid characters then echo invalid"

[[ $TITLE =~ ^[a-zA-Z0-9_-]{3,20}$ ]] && ret="VALID" || ret="INVALID"

echo $ret

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.