Solved

REGEX Help - Negative Words

Posted on 2014-04-01
5
304 Views
Last Modified: 2014-04-01
I have the following REGEX and it works for my purposes, but I want to exclude strings that look like this "http://twitter.com/share/" or "http://twitter.com/share" (no trailing slash).

How would I alter this REGEX to NOT match those strings?

https?://(?:www\.)?twitter.com/(?:#!/|(?!share)|(?<!share)|intent/user\?screen_name=)?(\w+)
0
Comment
Question by:EffinGood
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39971051
Perhaps this?
https?://(?:www\.)?twitter.com/(?!share)(?:#!/|intent/user\?screen_name=)?(\w+)

Open in new window


I'm not entirely clear on what you're trying to achieve with the other parts of the pattern. Please clarify if that's not what you want.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39971054
Tested, and seemingly working, here:
http://www.myregextester.com/?r=5b37e1fd
0
 

Author Comment

by:EffinGood
ID: 39971057
Hi Terry,

That was exactly what I needed! I had been putting "(?!share)" as an alternative using the pipe char, such as:
https?://(?:www\.)?twitter.com/(?:#!/|(?!share)|intent/user\?screen_name=)?(\w+)

Would you be a dear and explain to me why mine did not work and yours did? It would be ever so lovely and I'd consider you a hero for life.

Thank you!
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 39971102
Sure! You had an optional group being matched:
(?:#!/|(?!share)|intent/user\?screen_name=)?

Open in new window

The whole group is optional because there's a ? character after it. This means any twitter url would match as long as the (\w+) part of the pattern matches, including a url with "share" in it.

Rather than including the negative lookahead in the optional part, I moved it out to make it a required part of the pattern.

Up, up and away....
0
 

Author Closing Comment

by:EffinGood
ID: 39971162
Thanks mate! An answer and a 'splanation. Marvy!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question