Jump to content


Photo

match a string NOT containing this


  • Please log in to reply
8 replies to this topic

#1 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 17 June 2006 - 10:30 PM

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:

ls -l | grep -E [^mp3]

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 mp3, in that order, and all next to each other.

http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#2 homchz

homchz
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts
  • LocationLee, New Hampshire

Posted 17 June 2006 - 10:39 PM

what if you did this:

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



#3 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 17 June 2006 - 11:13 PM

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:
ls -l | grep -E "[^(mp3)]"

http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#4 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 17 June 2006 - 11:31 PM

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.
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#5 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 17 June 2006 - 11:53 PM

[!--quoteo(post=385150:date=Jun 17 2006, 06:31 PM:name=Wildbug)--][div class=\'quotetop\']QUOTE(Wildbug @ Jun 17 2006, 06:31 PM) View Post[/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 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.

Of course, I'm assuming it can be done - if not, just let me know! :)

http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#6 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 18 June 2006 - 02:56 PM

[!--quoteo(post=385159:date=Jun 17 2006, 07:53 PM:name=neylitalo)--][div class=\'quotetop\']QUOTE(neylitalo @ Jun 17 2006, 07:53 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Well, that's definitely helpful - thanks!

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.

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.
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#7 Guest_TSVictorT_*

Guest_TSVictorT_*
  • Guests

Posted 02 July 2006 - 04:37 PM

ls -l | grep -E [^mp3]$

This works fine,  it still shows the m3u files as well as jpg files that are in the same folder.


#8 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 04 July 2006 - 02:25 PM

ls -l | grep -E [^mp3]$
This works fine,  it still shows the m3u files as well as jpg files that are in the same folder.


That only means to show lines NOT ending in either "m", "p", or "3".  It doesn't mean to show lines NOT ending in "mp3".
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#9 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 04 July 2006 - 06:30 PM

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.

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
/^(?!.*?mp3)/is






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users