我一直试图识别一个页面的URL模式。对此,我遵循了下面的内容,但最后却出现了一个问题。
-> regex使用:
~((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)~i这已经确定了几乎所有类型的URL,如下所示
example.com
www.example.com
http://example.com
http://www.example.com
https://example.com
https://www.example.com但不幸的是,它还考虑了十进制值、价格值、电话号码、IP地址作为URL格式(可能我之前没有考虑过它们)。因此,为了解决这个问题,我已经习惯于在下面找到要排除的特定数值模式。
/^[0-9]+(\.[0-9]{1,})+\S+\w?$/使用此方法可以通过排除数值(如
决策值(1.11)
IP地址(123.123.123.123)
价格价值(11.11美元)
现在出现了新的问题“缩写也被认为是URL”。
世界卫生组织(按字母顺序排列)
那么,如何使用URL来标识PHP regex,从而排除上述问题的情况?
或
我是否可以使用PHP正则表达式来识别涉及缩写的单个字母值,如上面的示例?
谢谢
发布于 2017-01-03 10:07:35
您可以将这些排除放在一个负面的前景中,并使用
$re = '~(?x)\b # Word boundary
(?! # Exclusion list
[A-Z](?:\.[A-Z])+\b # No upper and 1+ sequences of . + an upper
| # or
\d+(?:\.\d+)+\S+\b # digits + 1+ dot and digits and 1+ non-whitespaces
)
(?:https?://)? # Optional http / https protocol part
(?:[-\w]+\.[-\w.]+)+ # 1+ sequences of 1+ - or word chars, then . and 1+ -, ., or word chars
\w(?::\d+)? # word char and 1 optional sequence of : and 1+ digits
(?:/(?:[-\w/.]*(?:\?\S+)?)?)* # 0+ sequences of /, 0+ -, word, /, . symbols, then 1 optional sequence of ? and 1+ non-whitespaces
\b~'; # word boundary
$str = 'example.com www.example.com http://example.com http://www.example.com https://example.com https://www.example.com Deciaml Values (1.11) IP Address (123.123.123.123) W.H.O Price values ($11.11)';
preg_match_all($re, $str, $matches);
print_r($matches[0]);https://stackoverflow.com/questions/41439919
复制相似问题