Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negating strings in Ruby regular expressions

Tags:

regex

ruby

I'm looking for a way to extract LinkedIn profile pages from lists of URLs using Ruby. Currently I am looping over the URLs and matching them against this regex:

/^http:\/\/.+\.linkedin.com\/(pub|in)/

However, the URLs of LinkedIn profile directory pages are as follows:

http://www.linkedin.com/pub/dir

, so I'm looking to avoid any links that have the pub/dir path in them. I know it's possible to negate character classes in Ruby regexs, such as [^abc] matching any character that isn't abc. Is there a way to do the same with strings? I.e. matching any sequence of characters besides "dir"?

like image 682
Richard Stokes Avatar asked Jul 24 '26 23:07

Richard Stokes


1 Answers

You can use a negative lookahead. Something like

(pub(?!\/dir)|in)
like image 84
mhyfritz Avatar answered Jul 26 '26 13:07

mhyfritz