I had a regex that detects when a string IS NOT the given pattern:
/\A((?!.*(www|http|@|\.com\b|\.net\b|\.de\b|\.info\b)).*)$/m
It means that strings like “hi you at http://www.test.com” is FALSE
But now I would liket to allow one URL: http://www.example.com.
It means:
” this is a test” > TRUE
“this is a test http://www.example.com” > TRUE
“this is a test example.com this” > TRUE
“this is a test http://www.test.com this” > FALSE
“this is a test test.com this” > FALSE
“this is a test test.com this http://www.example.com ” > FALSE (false because one forbiden URL is present)
It seems I need to include an AND operator, I think it is:
(?=.*PATTERN)
but unfortunately I couldn’t make it work.
ps: I know that this regex that detects urls is not perfect at all, but because my requirements it is bettern than nothing.
Thanks !
Edit:
My regex is located in a validator:
def self.validates_is_url_and_email_free(*attr_name)
validates_format_of attr_name, {:with => /\A((?!.*(www|http|@|\.com\b|\.net\b|\.de\b|\.info\b)).*)$/m, :message => I18n.t(:not_url_email)}
end
In this case using two separate expressions might be the solution:
It’s not clear what you’re doing here, but that does seem to be an extremely restrictive set of TLDs. There are literally thousands of valid TLDs which might need to be processed.