neylitalo Posted June 17, 2006 Share Posted June 17, 2006 This came up the other day when I wanted to find all non-MP3 files in a directory, short of using the file manager. This is what I ran:[code]ls -l | grep -E [^mp3][/code]But, as I discovered from many regex tutorials, [^abc] will match any string not containing a, b, OR c.So, what I'm looking for, is a regex that matches a string that doesn't contain [i]mp3[/i], in that order, and all next to each other. Quote Link to comment Share on other sites More sharing options...
homchz Posted June 17, 2006 Share Posted June 17, 2006 what if you did this:[code]ls -l | grep -E [^(mp3)][/code] Quote Link to comment Share on other sites More sharing options...
neylitalo Posted June 17, 2006 Author Share Posted June 17, 2006 No dice - it matches any string that doesn't contain (, m, p, 3, or ).It's not essential that I see the non-mp3 files in a command line, but I'd like to know how to do this. :)and actually, that exact command throws a syntax error - it should really be:[code]ls -l | grep -E "[^(mp3)]"[/code] Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 17, 2006 Share Posted June 17, 2006 Are you trying to do this in PHP or at the command line with grep?For command line, "ls -l | grep -v mp3$" seems to work just fine.From the man page:-v, --invert-match: Invert the sense of matching, to select non-matching lines.For PHP, preg_grep() has the following flag (from the manual):PREG_GREP_INVERT: If this flag is passed, preg_grep() returns the elements of the input array that do not match the given pattern . This flag is available since PHP 4.2.0. Quote Link to comment Share on other sites More sharing options...
neylitalo Posted June 17, 2006 Author Share Posted June 17, 2006 [!--quoteo(post=385150:date=Jun 17 2006, 06:31 PM:name=Wildbug)--][div class=\'quotetop\']QUOTE(Wildbug @ Jun 17 2006, 06:31 PM) [snapback]385150[/snapback][/div][div class=\'quotemain\'][!--quotec--] Are you trying to do this in PHP or at the command line with grep?For command line, "ls -l | grep -v mp3$" seems to work just fine.From the man page:-v, --invert-match: Invert the sense of matching, to select non-matching lines.For PHP, preg_grep() has the following flag (from the manual):PREG_GREP_INVERT: If this flag is passed, preg_grep() returns the elements of the input array that do not match the given pattern . This flag is available since PHP 4.2.0. [/quote]Well, that's definitely helpful - thanks!However, I'm still looking for a [b]regex[/b] way to match a string NOT containing a specific string; it's fine and dandy that grep and PHP can do it, but I'm looking for a way to do it within regex. Of course, I'm assuming it can be done - if not, just let me know! :) Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 18, 2006 Share Posted June 18, 2006 [!--quoteo(post=385159:date=Jun 17 2006, 07:53 PM:name=neylitalo)--][div class=\'quotetop\']QUOTE(neylitalo @ Jun 17 2006, 07:53 PM) [snapback]385159[/snapback][/div][div class=\'quotemain\'][!--quotec--]Well, that's definitely helpful - thanks!However, I'm still looking for a [b]regex[/b] way to match a string NOT containing a specific string; it's fine and dandy that grep and PHP can do it, but I'm looking for a way to do it within regex. Of course, I'm assuming it can be done - if not, just let me know! :)[/quote]Alright, if it has to be regex, for that particular pattern this might work: /[^m][^p][^3]$/There's also the negative lookbehind assertion (?<!): /(?<!mp3)$/Notice that these are anchored at the end of the line with "$". They'll work with your example because they mean "the end of the line not preceeded by mp3", but if you're just looking for something general to say "return true if the string doesn't contain this pattern" then I don't think there's a way to do that without the inversion flags I mentioned earlier. Quote Link to comment Share on other sites More sharing options...
Guest TSVictorT Posted July 2, 2006 Share Posted July 2, 2006 [code]ls -l | grep -E [^mp3]$ [/code]This works fine, it still shows the m3u files as well as jpg files that are in the same folder. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 4, 2006 Share Posted July 4, 2006 [quote author=TSVictorT link=topic=96163.msg390530#msg390530 date=1151858269][code]ls -l | grep -E [^mp3]$ [/code]This works fine, it still shows the m3u files as well as jpg files that are in the same folder. [/quote]That only means to show lines NOT ending in [b]either[/b] "m", "p", [b]or[/b] "3". It doesn't mean to show lines NOT ending in "mp3". Quote Link to comment Share on other sites More sharing options...
shoz Posted July 4, 2006 Share Posted July 4, 2006 [quote=neylitalo]However, I'm still looking for a regex way to match a string NOT containing a specific string; it's fine and dandy that grep and PHP can do it, but I'm looking for a way to do it within regex.[/quote]Using a negative lookahead at the beginning of the string, you can check that the string does not contain the pattern. Note that not all Regex engines support lookaheads/lookbehinds[code]/^(?!.*?mp3)/is[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.