Jump to content

match a string NOT containing this


neylitalo

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.
Link to comment
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]
Link to comment
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.
Link to comment
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! :)
Link to comment
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.
Link to comment
Share on other sites

  • 2 weeks later...
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.
Link to comment
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".
Link to comment
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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.