tags:

views:

195

answers:

3

I have text with file names scattered throughout. The filenames appear in the text like this:

|test.txt|
|usr01.txt|
|usr02.txt|
|foo.txt|

I want to match the filenames that don't start with usr. I came up with (?<=\|).*\.txt(?=\|) to match the filenames, but it doesn't exclude the ones starting with usr. Is this possible with regular expressions?

A: 

With python

>>> import re
>>>
>>> x="""|test.txt|
... |usr01.txt|
... |usr02.txt|
... |foo.txt|
... """
>>>
>>> re.findall("^\|(?!usr)(.*?\.txt)\|$",x,re.MULTILINE)
['test.txt', 'foo.txt']
S.Mark
+5  A: 
(?<=\|)(?!usr).*\.txt(?=\|)

You were nearly there :)

Now you have a positive lookbehind, and a positive and negative lookahead.

Tim Pietzcker
I struggled for a while using just one look-behind with this.. the trick was to use a combo. Well done sir.
Gishu
A: 
grep -v "^|usr" file

awk '!/^\|usr/' file

sed -n '/^|usr/!p' file
ghostdog74