Jump to content

Archived

This topic is now archived and is closed to further replies.

neylitalo

match a string NOT containing this

Recommended Posts

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.

Share this post


Link to post
Share on other sites
what if you did this:

[code]
ls -l | grep -E [^(mp3)]
[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--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! :)

Share this post


Link to post
Share on other sites
[!--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.

Share this post


Link to post
Share on other sites
Guest TSVictorT
[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.

Share this post


Link to post
Share on other sites
[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".

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.