1

I am looking for a regex which does not match "CVS". I don't want to use any feature which cannot translate into Emacs regex.

So far I have (in Python syntax, because I want to show it on regex101.com):

(^[^C].*|^.[^V].*|^..[^S].*)

This regex does not match "CVS" - so far so good. Unfortunately it does not match "CVS and more" either, but it should match it.

How can I adjust my regex to match "CVS and anything after it", but still not match "CVS"? (I.e how can I make the last test on the regex101.com page succeed?)

halloleo
  • 9,216
  • 13
  • 64
  • 122
  • is look behind supported in regex engine you're using ? – Code Maniac Jun 07 '19 at 02:07
  • is this what you're after, [check this](https://regex101.com/r/cW6qzK/3/tests) – Code Maniac Jun 07 '19 at 02:09
  • 1
    @CodeManiac To your 1st comment: I don't think Emacs support look-behind. – halloleo Jun 07 '19 at 02:14
  • @CodeManiac To your 2nd comment: Your regex does not succeed the first test (i.e. it *does* match `CVS` which it should not match). – halloleo Jun 07 '19 at 02:14
  • @Emma No. I need a regex which does *not* match the string "CVS", but matches the strings "CVS and more", "xVS", "CxS","CVx", "any other string", etc... (See the [tests on regex101.com](https://regex101.com/r/cW6qzK/2/tests): All test succeed, only the last one does not - to make that one succeed *as well*, is my question.) – halloleo Jun 07 '19 at 03:50
  • `M-x flush-lines CVS$` – Drew Jun 07 '19 at 15:51
  • @Drew I really need the regex, because in `ediff-directories` I want to use this regex to *exclude* the peski CVS directories littered around everywhere... – halloleo Jun 08 '19 at 12:46

1 Answers1

1

My understanding is that you want to match every individual line which isn't the line:

CVS

You're not too far off with your attempt.

Here the regex in Python syntax on regex101.com:

^(?:[^C\n]|C[^V\n]|CV[^S\n]|CVS.).*|^CV?$

And here's an elisp regexp in the read syntax for strings:

"^\\(?:[^C\n]\\|C[^V\n]\\|CV[^S\n]\\|CVS.\\).*\\|^CV?$"

Note the newlines. So in string syntax it becomes:

^\(?:[^C
]\|C[^V
]\|CV[^S
]\|CVS.\).*\|^CV?$

n.b. You can use M-x re-builder to test these in Emacs.

phils
  • 71,335
  • 11
  • 153
  • 198
  • Cool. Makes sense. Will test it out tomorrow. – halloleo Jun 08 '19 at 12:48
  • Just noticed the regex is still not right. Look at the string "CV" (without the S) - I want the regex to match it, but it doesn't match. Check the added test on the updated [regex101 page](https://regex101.com/r/cW6qzK/7). – halloleo Jun 12 '19 at 07:18
  • 1
    Right you are. Obviously `C` on its own would fail as well. Let's suffix that regexp with `\|^CV?$`. Answer updated. – phils Jun 12 '19 at 07:51
  • I've added a mention of `re-builder` as well. If you're unfamiliar with that, you may also find this useful: https://emacs.stackexchange.com/q/5568/454 – phils Jun 12 '19 at 07:55
  • Sorry for late response. Yes, all working now. Even "C" matches correctly (although it is not mentioned as an explicit cas in the regex). – halloleo Jun 16 '19 at 13:21
  • 1
    Note that `CV?` means `C` *optionally* followed by `V`, so that covers both cases. – phils Jun 16 '19 at 14:42