0

I am trying to match the string ".php" but only when "share" is not in front of the ".php". So, for example, "test.php" should be found, but "share.php" should be skipped.

I have this RegEx but it seems to not be working when I think it should. I think the "." is what is messing me up.

(?!.*share)\.php

Can anyone give some assistance?

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
Dennis
  • 708
  • 2
  • 10
  • 25

4 Answers4

9

Your negative lookbehind assertion is incorrectly framed. The following should work

(?<!share)\.php
iruvar
  • 22,736
  • 7
  • 53
  • 82
  • I just tried this code on "test.php" and "share.php" and neither were matched. test.php should have been matched, no? but, it does seem to work if you drop the code in here: http://www.rubular.com/r/faLGlVEDjP Odd. Maybe something is wrong with the regex checker in Visual Studio?? – Dennis May 16 '13 at 18:34
  • @Dennis, what language are you using? It worked for me, take a look at http://www.rubular.com/r/vq9fVIpNvt – iruvar May 16 '13 at 18:35
  • Yeah, i see that. Odd it is not working in Visual Studio?! I am using it to find code chunks in a project and the share.php exists on hundreds of pages, but other links with anything.php are very few. Was hoping not to have to brute-force it!! :( – Dennis May 16 '13 at 18:41
  • @Dennis, per http://stackoverflow.com/q/9022258/753731, Visual Studio supports non-standard regex syntax, you may want to look at the accepted answer there. I have no means of testing it. – iruvar May 16 '13 at 18:51
2

These are 100% confirmed working solutions:

If you are using Visual Studio, this is the code to achieve this:

~(share)\.php

If you are just looking for a typical RegEx (not with Visual Studio), this works:

(?<!share)\.php

Thanks for the input all!

Dennis

Dennis
  • 708
  • 2
  • 10
  • 25
0

Looks like you're trying to use a negative lookahead. Correct regex for that case would be:

^(?!share)[^.]+\.php$

Live Demo: http://www.rubular.com/r/wFmMH8E2WI

Using lookbehind:

^[^.]+(?<!share)\.php$
anubhava
  • 761,203
  • 64
  • 569
  • 643
-2
[^(share)]\.php 

how about that?

Alternative if you are using grep

ls | grep \.php$ | grep -v share\.php
beiller
  • 3,105
  • 1
  • 11
  • 19
  • 3
    This is wrong. You regex will check for each character in "share"; even "s.php" will not be recognized. What you need is a negative lookbehind. – Roney Michael May 16 '13 at 17:47