Solved

REGEX Help - Negative Words

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now