How can I specify a regex pattern which matches any line without
"lecture"
anywhere in it?".pdf"
at the end?
How can I specify a regex pattern which matches any line without
"lecture"
anywhere in it?
".pdf"
at the end?
For your actual problem, per the comments, M-x keep-lines
is what you're looking for, and in your case you would keep lines matching the regexp lecture\|\.pdf$
As Drew comments, you can't do what you've actually asked for with a regular expression in Emacs, as PCRE-style arbitrary zero-width look-ahead assertions are not available.
If the pattern is anchored to the beginning of the string, then it becomes practical (but still not easy) to match "strings which do not start with X". The following is a regexp which matches lines which do not start with lecture
(and are not, in their entirety, a prefix of that word):
^\([^l]\|l[^e]\|le[^c]\|lec[^t]\|lect[^u]\|lectu[^r]\|lectur[^e]\).*
You get the idea.
In rare cases that's useful, but more often you would do what Drew outlined:
Typically, if you need the opposite of what a regexp matches you find what it matches and subtract that from the original search space to get the complement.
You might also find this trick interesting:
M-x query-replace-regexp
and replace .*
with:
\,(if (string-match-p "lecture\\|\\.pdf$" \&) \& "")
Note that this is matching and replacing all lines -- but the ones which match the embedded regexp are 'replaced' with identical text, and the ones which don't are replaced with an empty string.
Adding a newline C-qC-j to the end of that .*
search pattern would produce the same textual result as keep-lines
with lecture\|\.pdf$
C-h i g (elisp)Syntax of Regexps
– phils
Oct 02 '18 at 03:49
flush-ilnes
andkeep-lines
.) Typically, if you need the opposite of what a regexp matches you find what it matches and subtract that from the original search space to get the complement. But just what you're trying to do makes a difference in how you might want to proceed. – Drew Oct 01 '18 at 21:53regex-replace
. – Tim Oct 01 '18 at 22:08M-x keep-lines
which you're wanting. This lets you specify the patternlecture\|\.pdf$
for the lines you want to keep, and all non-matching lines are deleted. – phils Oct 01 '18 at 23:10.*lecture.*
into a DFA, then negating that DFA, and then turning that negated DFA back into a regexp. – Stefan Oct 02 '18 at 02:41