?
Solved

REGEX Help - Negative Words

Posted on 2014-04-01
5
Medium Priority
?
316 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

864 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