引:在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或替换那些符合某个模式的文本内容。许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎。正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。正则表达式通常缩写成“regex”,单数有regexp、regex,复数有regexps、regexes、regexen。
功能:运用正则表达式,可以找寻到指定格式,按照表达式所规范的语法字符串片段检查。
运用实例描述:
抓取资料,提取json格式的字符片段,并解析,存储到数据库中。当中使用了WebRequest等类库进行获取网页信息;Regex类库进行解析提取;JsonTextParser等类库进行解析json格式的变量;SqlConnection等进行数据库传输。性能和解析程度都比较高,但是要求正则表达式的设计能力较高。
以下是代码部分:
#region address
static void downloadadd(SqlConnection conn)
{
SqlConnection connn = new SqlConnection("Data Source=192.168.0.2;Initial Catalog=HKSO;Persist Security Info=True;User ID=zhisheng;pwd=password.123");
connn.Open();
SqlCommand sqlcmd = connn.CreateCommand();
sqlcmd.CommandText = "SELECT distinct [ceref] FROM [HKSO].[dbo].[Corporation]";
SqlDataReader dr = sqlcmd.ExecuteReader();
dr.Read();
#region repeat in name
while (dr.Read())
{
#region get content
WebRequest request = WebRequest.Create("http://www.sfc.hk/publicregWeb/corp/" + dr["ceref"] + "/co");
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
#endregion
#region define values
string tel = "";
string fax = "";
string email = "";
string fullAddress = "";
string fullAddressChin = "";
string centralEntity = "";
#endregion
#region get json
Regex regex = new Regex(@"\[\{().*\}\]");
MatchCollection mth = regex.Matches(responseFromServer);
foreach (Match m in mth)
{
try
{
JsonTextParser parser = new JsonTextParser();
JsonObject obj = parser.Parse(m.Value);
List<JsonObject> newfiled = (List<JsonObject>)obj.GetValue();
for (int k = 0; k < newfiled.Count; k++)
{
foreach (JsonObject field in newfiled[k] as JsonObjectCollection)
{
switch (field.Name)
{
case "tel":
tel = (field.GetValue() == null ? "null" : field.GetValue().ToString());
break;
case "fax":
fax = (field.GetValue() == null ? "null" : field.GetValue().ToString());
break;
case "email":
email = (field.GetValue() == null ? "null" : field.GetValue().ToString());
break;
case "address":
{
foreach(JsonObject add in field as JsonObjectCollection)
{
switch (add.Name)
{
case "fullAddress":
fullAddress = (add.GetValue() == null ? "null" : add.GetValue().ToString());
break;
case "fullAddressChin":
fullAddressChin = (add.GetValue() == null ? "null" : add.GetValue().ToString());
break;
case "centralEntity":
centralEntity = (add.GetValue() == null ? "null" : add.GetValue().ToString());
break;
default :
break;
}
}
}
break;
default:
break;
}
}
//
SqlCommand cmd = new SqlCommand("insert dbo.Address values ('" + dr["ceref"] + "','" + tel + "','" + fax + "','" + email + "','" + fullAddress + "','" + fullAddressChin + "','" + centralEntity + "')", conn);
cmd.ExecuteNonQuery();
}
}
catch (Exception exx)
{
}
finally
{
}
}
#endregion
}
#endregion
connn.Dispose();
connn.Close();
}
#endregion
小记:
在C#中使用正则表达式,调用regex函数要注意一些转换
(1)@“”字符串
(2)"要用""
(3)<>/#@$等符号要在前面加\