php - Regex equals condition except for certain condition -
i have written following regex in php use within preg_replace().
/\b\s*(.com|.net|.us|.biz|.org|.info|.xxx|.mx|.ca|.fr|.in|.cn|.hk|.ng|.pr|.ph|.tv|.ru|.ly|.de|.my|.ir)\s*\b/i
this regex removes urls string pretty far (though sure can write better one). need able add exclusion though specific domain. pseudo code this:
if string contains: .com or .net or. biz etc... , not contain: foo.com execute condition.
any idea on how this?
just add negative lookahead assertion:
/(?<=\s|^)(?!\s*foo\.com)\s*\.(com|net|us|biz|org|info|xxx|mx|ca|fr|in|cn|hk|ng|pr|ph|tv|ru|ly|de|my|ir)\s*\b/im
also, remember need escape dot - , can move outside alternation since each of alternatives starts dot.
Comments
Post a Comment