Solved

REGEX Help - Negative Words

Posted on 2014-04-01
5
305 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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 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