LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 05-18-2022, 03:06 AM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
regex condition matching anything except one of given patterns


How can I construct a regex condition that matches anything except one of the given patterns?

Last edited by Faki; 05-18-2022 at 03:48 AM.
 
Old 05-18-2022, 03:23 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
obviously it depends on the language you use.
The usual way is something like this:
Code:
if sample ~ pattern
then
    do something
else
    do something else
fi
And now you can just skip the "then" part.
 
Old 05-18-2022, 03:50 AM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
I want to specifically know if there is a not match regex operator when I am interested in not matching only. In case of bash scripts.
 
Old 05-18-2022, 03:55 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
the not match is problematic, especially if you have more than one patterns.
 
Old 05-18-2022, 09:21 AM   #5
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by Faki View Post
I want to specifically know if there is a not match regex operator when I am interested in not matching only. In case of bash scripts.
There is a regex concept called negative lookaheads, which succeeds when its sub-expression cannot match.

Bash's regex engine doesn't support these, but both Perl's and Python's do.

There might potentially be workarounds depending on what specifically you are trying to do, but - given the frequency you've been asking regex questions - you need to consider if Bash is the right tool - and indeed whether regex is enough for whatever you're trying to achieve. (Some of your questions suggest you may want to look at Antlr/Bison/etc.)


Last edited by boughtonp; 05-18-2022 at 09:22 AM.
 
Old 05-18-2022, 02:08 PM   #6
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Quote:
How can I construct a regex condition that matches anything except one of the given patterns?
Code:
a=(Mary had a little lamb)
echo "${a[@]%Mary}"
echo "${a[@]%a}"
echo "${a[@]%little}"
Doesn't have to be done in one line.

Examples:

Code:
a=(1 2 3 4 5 6 7 8 9 0 a b c d e f g)

for i in "${a[@]}"; do
    if [[ "$i" != [0-9] ]]; then
        echo ""$i" in not a digit"
    else
        echo ""$i" is a digit"
    fi
done

for i in "${a[@]}"; do
    if [[ "$i" != [a-e] ]]; then
        echo "$i"
    fi
done

for i in "${a[@]}"; do
    if [[ "$i" != [037de] ]]; then
        echo "$i"
    fi
done
Code:
a="Mary had a little lamb"

if [[ "$a" =~ ^Mary ]]; then 
    echo ""$a", starts with Mary"
fi

if [[ "$a" != ^Bob ]]; then
    echo ""$a", does not start with Bob"
fi
Code:
a="How can I construct a regex condition 
that matches anything except one of the 
given patterns?"

while read line; do 
    if [[ "$line" != "given patterns?" ]]; then
        echo "$line"
    fi
done <<< "$a"
 
Old 05-18-2022, 04:24 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Attention:
!= is the negation of == and takes shell globs.
!~ would be the negation of =~ but bash does not yet support it.

Corrections:
Code:
if [[ "$a" != Bob* ]]; then
    echo "'$a' does not start with Bob"
fi
Code:
if ! [[ "$a" =~ ^Bob ]]; then
    echo "'$a' does not start with Bob"
fi
Code:
    if [[ "$line" != *"given patterns?"* ]]; then
        echo "$line"
    fi
Code:
    if ! [[ "$line" =~ "given patterns?" ]]; then
        echo "$line"
    fi
 
  


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
Matching patterns or partial pattern matching yaplej Programming 6 12-16-2012 10:21 AM
[SOLVED] Search multiple patterns & print matching patterns instead of whole line Trd300 Linux - Newbie 29 03-05-2012 07:41 PM
[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 only matching single-character regex patterns? Lordandmaker Programming 3 01-20-2009 08:59 AM
Remembering patterns and printing only those patterns using sed bernie82 Programming 5 05-26-2005 05:18 PM

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

All times are GMT -5. The time now is 06:11 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