regexp: match string that contains list of chars

Tags
beginners

 

 
Thread Tools Search this Thread
# 1  
Old 10-14-2010
regexp: match string that contains list of chars

Hi,

I'm curious about how to do a very simple thing with regular expressions that I'm unable to figure out.

If I want to find out if a string contains 'a' AND 'b' AND 'c' it can be very easily done with grep:
Code:
echo $STRING|grep a|grep b|grep c

but, how would you do that in a single regexp?

A possible solution would be:

Code:
/a.*b.*c|a.*c.*b|b.*c.*a|b.*a.*c|c.*b.*a|c.*a.*b/

but it's so ugly it's nasty!! (imagine if instead of 3 chars we have 10!)

any gurus out there know how to do this?

cheers

Last edited by vbe; 10-14-2010 at 11:12 AM.. Reason: code tags...
# 2  
Old 10-14-2010
Code:
awk '/a/&&/b/&&/c/'

# 3  
Old 10-14-2010
Scrutinizer: that's very cool, it's better than chaining up grep's Smilie But I would like to do that in a single regexp i.e. without the use of shell tools like grep, awk, sed...
# 4  
Old 10-14-2010
Well, awk has not one regex but one command, which is nicer formatted, and you can use sed, which is faster, generally and because it doe not evaluate regex if the line is done=dead, and the list of patterns can be of any length, easily viewed:

Code:
sed '
  /a/!d
  /b/!d
  /c/!d
 '



---------- Post updated at 10:45 AM ---------- Previous update was at 10:42 AM ----------

If you want just regex not commands, you are probably out of luck. The searches are too unrelated for one regex. What context do you want to use it in, if not a command?
This User Gave Thanks to DGPickett For This Post:
Scrutinizer (10-14-2010)
# 5  
Old 10-14-2010
If you want to use return codes (like grep -q)
Code:
awk '/a/&&/b/&&/c/{f=1;exit}END{if(!f){exit 1}}'

# 6  
Old 10-14-2010
Quote:
Originally Posted by DGPickett
If you want just regex not commands, you are probably out of luck. The searches are too unrelated for one regex. What context do you want to use it in, if not a command?
For example in any programming language that supports pcre: C, perl, python, Ruby... of course every programming language has other ways to check this, for example in python:
Code:
>>> s = "axbxc"
>>> 'a' in s and 'b' in s and 'c' in s
True

I just want to know if it's possible to do that in a single regular expression, just out of curiousity and simply to get a better understanding of regexps.

I tried to do it like this:

Code:
/([abc]).*([^\1]).*[^\2]/

But of course that doesn't work because [^\1] matches *all* the characters except the character that matched in the first parenthesis set... I think this should be done with some kind of backtracking.

And BTW I'm sure that it can be done with regexps! I mean, if you can test if a number is a primer number with regular expressions, I refuse to believe this simple thing can't be done Smilie
# 7  
Old 10-14-2010
Well, if you dislike but must apply the three regex in sequence, you might try this simple heurism: Put the three regex in as list, and apply them in the current order; if one misses, removing a line from contention, move it to the top of the list if not already, sliding the others down. For instance, consider c e q as three regex. The q will reject more lines than c, usually, and the e less, but by letting the best rejecters float to the top, you save a lot of second and third regex searches.

I have a name for this, but it is not politically correct, something about how a dictator selects a military commander -- death at first failure. It came to me one day as a text editor took very long to find instances of 'equal': it did a character scan and for every first character, it stopped and did a sting compare, tragically missing the filtering power of q. If I searched for 'qual', it was quick (Borland Sprint on I386 dos emulation under UNIX SVR3).
 

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
sed - print only the chars that match a given set in a string naderra Shell Programming and Scripting 1 08-25-2018 12:54 PM
sed to remove newline chars based on pattern mis-match chill3chee Shell Programming and Scripting 6 07-08-2016 01:02 AM
find and Replace String in Perl - Regexp cillmor Shell Programming and Scripting 2 10-31-2015 01:10 PM
Add an string at every x chars kadu Shell Programming and Scripting 9 12-06-2014 10:27 AM
How to search for a string with special chars? santokal UNIX for Dummies Questions & Answers 8 02-19-2014 06:37 PM
Regexp for string that might contain a given character jnojr Shell Programming and Scripting 1 08-30-2013 04:09 PM
How to use regexp to find an ipaddress from a query string? ampak Shell Programming and Scripting 3 04-22-2013 08:59 PM
Question on TCL regexp and match mar85 Shell Programming and Scripting 2 12-05-2012 04:09 AM
[Solved] print chars of a string tafazzi87 Shell Programming and Scripting 10 11-03-2011 07:45 AM
Repeatable chars in a string BeefStu Shell Programming and Scripting 3 10-04-2010 02:46 AM
extract string until regexp from backside elifchen Shell Programming and Scripting 8 05-26-2010 09:20 AM
perl regexp: no match across newlines BatManWSL Shell Programming and Scripting 2 05-17-2010 03:19 AM
Retreive string between two chars danland Shell Programming and Scripting 25 06-25-2007 04:02 PM
regexp to get first line of string jonas.gabriel Shell Programming and Scripting 3 05-09-2007 04:46 AM
RegExp negative match not working umen Shell Programming and Scripting 2 08-22-2006 05:57 PM