LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-22-2018, 01:47 PM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 193

Rep: Reputation: 35
RegEx in if condition. How?


Hello

I trying to RegEx mpg and mpeg in 'if' condition. It is not working.

Code:
if [[ $VAR == "mp4" || $VAR =~ "mp[e]?g" ]] ; then
  echo "Do something"
fi
assuming VAR could have mpg or mpeg value.

How to do that?

Thanks
 
Old 08-22-2018, 02:02 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,228
Blog Entries: 11

Rep: Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177
Assuming you are using the bash shell, remove the quotes from the regex expression. Quoting all or parts of the regex causes the quoted parts to be matched as a string (man bash of course).

Code:
if [[ $VAR == "mp4" || $VAR =~ mp[e]?g ]] ; then
  echo "Do something"
fi
I am not sure how this applies to other shells, but likely similar, see the respective man pages as always.

Last edited by astrogeek; 08-22-2018 at 02:14 PM.
 
2 members found this post helpful.
Old 08-22-2018, 02:18 PM   #3
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 193

Original Poster
Rep: Reputation: 35
Quote:
Originally Posted by astrogeek View Post
Assuming you are using the bash shell, remove the quotes from the regex expression. Quoting all or parts of the regex causes the quoted parts to be matched as a string (man bash of course).

Code:
if [[ $VAR == "mp4" || $VAR =~ mp[e]?g ]] ; then
  echo "Do something"
fi
I am not sure how this applies to other shells, but likely similar, see the respective man pages as always.
That worked perfectly. Thanks.
 
Old 08-23-2018, 03:09 AM   #4
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 854

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by ddenial View Post
Code:
if [[ $VAR == "mp4" || $VAR =~ "mp[e]?g" ]] ; then
  echo "Do something"
fi
By the way, square brackets are not necessary around "e" as there is only one character inside.
So:
Code:
$VAR =~ "mp[e]?g"
can be changed with:
Code:
$VAR =~ "mpe?g"
 
1 members found this post helpful.
Old 08-23-2018, 08:59 AM   #5
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 193

Original Poster
Rep: Reputation: 35
Quote:
Originally Posted by l0f4r0 View Post
By the way, square brackets are not necessary around "e" as there is only one character inside.
So:
Code:
$VAR =~ "mp[e]?g"
can be changed with:
Code:
$VAR =~ "mpe?g"
Thanks
 
Old 08-23-2018, 03:38 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,204

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
A glob match (==) is "anchored" by default, so you might want to put anchors on the RE match (=~)
Code:
if [[ $VAR == mp4 || $VAR =~ ^mpe?g$ ]]
Compare with the following, where both matches are "floating"
Code:
if [[ $VAR == *mp4* || $VAR =~ mpe?g ]]
Last but no least, the RE is an ERE, and is more powerful than the glob.
Often you can do everything in one ERE
Code:
if [[ $VAR =~ ^(mp4|mpe?g)$ ]]

Last edited by MadeInGermany; 08-23-2018 at 03:48 PM.
 
1 members found this post helpful.
Old 08-23-2018, 07:18 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,146

Rep: Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935
All good advice, but I wonder how expensive the regex engine is ?. Might be better off using the OR test if there is a large disparity in favour of "mp4" and the test can take advantage of short-circuiting.
Just idle musings ...
 
Old 08-24-2018, 01:07 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,103

Rep: Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145
yes, a case would be probably better:
Code:
case $VAR in
    mp4)
    mpeg)
    mpg)
    *)
esac
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Confusing issue with Perl regEx - Regex check seems to require variable being set EnderX Programming 1 09-07-2013 04:36 AM
check Negative egrep condition in an if condition novicunix Programming 5 02-02-2013 12:52 AM
Condition in cp/ls | grep (regex, now I have two problems) Freddythunder Linux - Newbie 6 07-06-2012 08:39 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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:52 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration