[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

REGEX Help - Negative Words

Posted on 2014-04-01
5
Medium Priority
?
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

650 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