在jsp中怎么使用正则表达式!可以把数据库中提出的Ip最后的几位替换成*
127.0.0.1 替换成 127.0.*.*
这样吧,其实你应该把 这种转换写到Action或者Bean中。
(这里假设getIP返回的是ip字符串)
[code="xml"]<%=((类型)request.getAttribute("message")).getIP().replaceAll("(\d{1,3}).(\d{1,3}).\d{1,3}.\d{1,3}","$1.$2.*.*")%>[/code]
关于IP地址的正则表达式检测方法如下:
[code="java"]
/**
[color=blue][b]如下方式可以的。[/b][/color]
[code="java"] public static void main(String[] args) throws Exception {
String str = "192.168.2.100";
String result = str.replaceAll("(\\d{1,3}).(\\d{1,3}).(\\d{1,3}).(\\d{1,3})","$1.$2.*.*");
System.out.println(result);
}[/code]
[b]输出:
[/b]
[quote]192.168.*.*[/quote]
[b]PS: 因为:[/b]
[quote]可以把数据库中提出的Ip最后的几位替换成* [/quote]
[b]这里就不再对IP进行校验了。[/b]
[code="xml"]<%= "192.168.2.100".replaceAll("(\d{1,3}).(\d{1,3}).\d{1,3}.\d{1,3}","$1.$2.*.8") %>[/code]
可以吧?
这样:
[code="xml"]<%= "192.168.2.100".replaceAll("(\d{1,3}).(\d{1,3}).\d{1,3}.\d{1,3}","$1.$2.*.*") %>[/code]
试试:
[code="xml"]${message.ip.replaceAll("(\d{1,3}).(\d{1,3}).\d{1,3}.\d{1,3}","$1.$2.*.*") [/code]
(\d{1,3}.\d{1,3}).\d{1,3}.\d{1,3}
换
\1.*.*