LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Regex help (http://www.linuxquestions.org/questions/programming-9/regex-help-4175427921/)

the_gripmaster 09-18-2012 09:37 PM

Regex help
 
This is just a simple script which echoes to the screen whatever the user types and quits when the user types quit or QUIT.

Code:

while read LINE; do
  if [[ "$LINE" =~ quit|QUIT ]]; then
    break
  fi
  echo $LINE
done

How do I add case insensitivity to the quit word so that typing in quit, QuIt, Quit, or any other possibility matches the if statement?

tc_ 09-18-2012 10:21 PM

Try
Code:

if [[ $(echo $LINE | tr [:lower:] [:upper:]) = "QUIT" ]]; then
    break
fi

See also the man page of 'tr'.

firstfire 09-18-2012 10:35 PM

Hi.

You can also use
Code:

shopt -s nocasematch
to enable case-insensitive matching:

man bash:
Quote:

nocasematch
If set, bash matches patterns in a case-insensitive fashion when performing matching while executing case or [[ conditional commands.

grail 09-19-2012 10:53 AM

Or if bash is version 4 or greater:
Code:

[[ ${LINE^^} == QUIT ]] && break


All times are GMT -5. The time now is 02:25 PM.