LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-18-2012, 09:37 PM   #1
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Rep: Reputation: 38
Question 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?
 
Old 09-18-2012, 10:21 PM   #2
tc_
LQ Newbie
 
Registered: Sep 2010
Location: Germany
Distribution: Slackware
Posts: 28

Rep: Reputation: 30
Try
Code:
if [[ $(echo $LINE | tr [:lower:] [:upper:]) = "QUIT" ]]; then
    break
fi
See also the man page of 'tr'.
 
Old 09-18-2012, 10:35 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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.
 
Old 09-19-2012, 10:53 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Or if bash is version 4 or greater:
Code:
[[ ${LINE^^} == QUIT ]] && break
 
  


Reply

Tags
bash, regex



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
New to regex, need help aiman Programming 11 08-26-2011 06:28 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Need a regex, I suck at regex's d3funct Programming 4 02-25-2002 08:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration