关键字:正则表达式匹配聊天中所有url连接地址-高亮显示
需要说明:聊天时,发送内容中包含的连接地址进行高亮显示
2、正则表达式:
一个能识别域名url的表达式:
wangshang2.bds=((http[s]{0,1}|ftp|rtsp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)
一个能识别域名和ip等url的表达式(不带http等前缀的纯ip不能识别):
wangshang3.bds=((http[s]{0,1}|ftp|rtsp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-z0-9]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)
一个能识别域名和ip等url的表达式(可识别不带http等前缀的纯ip)
wanghui.bds=((https|http|ftp|rtsp|mms):\\/\\/)[^\\s]+([a-zA-z0-9])|(([0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?|((http|ftp|https)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+[\\.|\\/]([a-zA-Z]{1,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)
一个能识别域名和ip等url表达式+域名时后缀限制(该表达式可能存在编码问题,仅供参考,用起来报错 没找到是那个字符的原因) wangshang.bds =((((ftp:|https:|http:)([\Q/\\E])*)|())(((%[0-9a-fA-F][0-9a-fA-F])|([a-zA-Z0-9])|([\Q$-_.+!*'(),;?&=\E]))+(:((%[0-9a-fA-F][0-9a-fA-F])|([a-zA-Z0-9])|([\Q$-_.+!*'(),;?&=\E]))*)?@)?(((((([a-zA-Z0-9]){1}([a-zA-Z0-9\-])*([a-zA-Z0-9]{1}))|([a-zA-Z0-9]))\.)+(biz|com|edu|gov|info|int|mil|name|net|org|pro|aero|cat|coop|jobs|museum|travel|arpa|root|mobi|post|tel|asia|geo|kid|mail|sco|web|xxx|nato|example|invalid|test|bitnet|csnet|onion|uucp|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw))|([0-9]{1,3}(\.[0-9]{1,3}){3}))(\:([0-9]+))?(([\Q/\\E])+((((%[0-9a-fA-F][0-9a-fA-F])|([a-zA-Z0-9])|([\Q$-_.+\!*'(),;:@&=\E]))*)(([\Q/\\E])*((%[0-9a-fA-F]{2})|([a-zA-Z0-9])|([\Q$-_.+\!*'(),;:@&=\E]))*)*)(\?((%[0-9a-fA-F]{2})|([a-zA-Z0-9])|([\Q$-_.+!*'(),;:@&=<>#"{}[] ^`~|\/\E]))*)*)*) 3、Java代码效果 public class TestDomain { public static void main(String[] args) { String strContent="请到以下地址查询:查询地址:充值地址:https://pay.sdo.com/Index.aspx?type=card尊敬的用户: 您好,该卡帐号和密码是正确的,请到以下https://pay.sdo.com/Index.aspx?type=card 地址查询:查询地址:充值地址:https://pay.sdo.com/Index.aspx?type=card " +" 192.168.1.888" +" 192.168.10.9" +" www.zs.com " +" ftp://192.168.10.8" +" www.aa.bb" +" rtsp://192.168.10.8" +" http://192.168.1.99" +" http://pay.sdo.com/Index.aspx?type=card" +" www.ff.com"; System.out.println(strContent); String regex = getS("wangshang3.bds"); String regex2 = "(http:|https:)//[^[A-Za-z0-9\\._\\?%&+\\-=/#]]*"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(strContent); StringBuffer result = new StringBuffer(); while (matcher.find()) { String urlStr=matcher.group(); StringBuffer replace = new StringBuffer(); replace.append("<a href=\"").append(urlStr); replace.append("\" target=\"_blank\">"+urlStr+"</a>"); matcher.appendReplacement(result,replace.toString()); System.out.println(urlStr); } matcher.appendTail(result); System.out.println(result); } private static String getS(String name){ ResourceBundle resource = ResourceBundle.getBundle("matcher"); String val = resource.getString(name); return val; } }