【转载】正则表达式 地址匹配(地区城市地址)
项目中需要对商户所在地址进行匹配,关键是取出用户所在路名,用户的地址是已经有的,只要从现有的地址中筛选出路名就可以。下面是Javascript的代码实现:
Figure1: 6s
Figure2:3s
<script>
var reg = /(.*?)[市区弄号楼路道]/ig;
var address0 = "上海市国定路杨浦区515号";
var address1 = "徐汇区斜土路2421号4号楼孔家花园内";
var address2 = "上海浦东新区陆家嘴西路168号正大广场6楼11-13号";
var address3 = "陆家嘴西路168号正大广场6楼11-13号";
var address4 = "上海浦东新区浦东大道728号海神诺富特大酒店50楼";
var address5="上海浦东张江地铁附近"
var address = address2;
//先将地址按[市区弄号楼路道]进行分组
var result = address.match(reg);
if(result)
{
//在分组中查找匹配的结果
for(var i=0;i< result.length;i++)
{
if(result[i].match(/(.+?)[路道]/))
{
address=result[i];
}
}
}
alert(address);
</script>
var reg = /(.*?)[市区弄号楼路道]/ig;
var address0 = "上海市国定路杨浦区515号";
var address1 = "徐汇区斜土路2421号4号楼孔家花园内";
var address2 = "上海浦东新区陆家嘴西路168号正大广场6楼11-13号";
var address3 = "陆家嘴西路168号正大广场6楼11-13号";
var address4 = "上海浦东新区浦东大道728号海神诺富特大酒店50楼";
var address5="上海浦东张江地铁附近"
var address = address2;
//先将地址按[市区弄号楼路道]进行分组
var result = address.match(reg);
if(result)
{
//在分组中查找匹配的结果
for(var i=0;i< result.length;i++)
{
if(result[i].match(/(.+?)[路道]/))
{
address=result[i];
}
}
}
alert(address);
</script>
替换的效率测试(MS VS MY)
因为进来公司要给其他公司提供数据,数据中有字符要替换。有两种替换方法
代码1:
public static void Main()
{
string input1 = "";
StringBuilder sb1 = new StringBuilder();
for(int i =0 ; i < 50000; i++)
{
sb1.Append("wo\r");
}
input1 = sb1.ToString();
WL("sb Test Date is complate..");
Stopwatch sw1 = new Stopwatch();
//char
sw1.Start();
StringBuilder sb = new StringBuilder();
if (input1 != String.Empty)
{
for (int i = 0; i < input1.Length; i++)
{
switch (input1[i])
{
case (char)13:
sb.Append(" ");
break;
case (char)10:
sb.Append(" ");
break;
default:
sb.Append(input1[i]);
break;
}
}
input1 = sb.ToString();
}
sw1.Stop();
WL(sw1.ElapsedMilliseconds+ "毫秒");
RL();
}
{
string input1 = "";
StringBuilder sb1 = new StringBuilder();
for(int i =0 ; i < 50000; i++)
{
sb1.Append("wo\r");
}
input1 = sb1.ToString();
WL("sb Test Date is complate..");
Stopwatch sw1 = new Stopwatch();
//char
sw1.Start();
StringBuilder sb = new StringBuilder();
if (input1 != String.Empty)
{
for (int i = 0; i < input1.Length; i++)
{
switch (input1[i])
{
case (char)13:
sb.Append(" ");
break;
case (char)10:
sb.Append(" ");
break;
default:
sb.Append(input1[i]);
break;
}
}
input1 = sb.ToString();
}
sw1.Stop();
WL(sw1.ElapsedMilliseconds+ "毫秒");
RL();
}
代码2:
public static void Main()
{
string input1 = "";
StringBuilder sb1 = new StringBuilder();
for(int i =0 ; i < 50000; i++)
{
sb1.Append("wo\r");
}
input1 = sb1.ToString();
WL("replace Test Date is complate..");
Stopwatch sw1 = new Stopwatch();
//char
sw1.Start();
input1 = input1.Replace("\r"," ");
input1 = input1.Replace("\n"," ");
sw1.Stop();
WL(sw1.ElapsedMilliseconds+ "毫秒");
RL();
}
{
string input1 = "";
StringBuilder sb1 = new StringBuilder();
for(int i =0 ; i < 50000; i++)
{
sb1.Append("wo\r");
}
input1 = sb1.ToString();
WL("replace Test Date is complate..");
Stopwatch sw1 = new Stopwatch();
//char
sw1.Start();
input1 = input1.Replace("\r"," ");
input1 = input1.Replace("\n"," ");
sw1.Stop();
WL(sw1.ElapsedMilliseconds+ "毫秒");
RL();
}
Figure1: 6s
Figure2:3s