假如想在如下的一段文字里,要将所有诸如" 'Product2'=>GUID "格式的文字全部删除,就可以用正则表达式来实现。
Guid="<%= {'Product1'=>{'Win32'=>'5DCE296C-352B-4DA6-9105-03047B5135AA','x64'=>'ED01447D-ACF2-4006-95CD-1194C6965EF5'},
'Product2'=>{'Win32'=>'AB7E192A-5FCF-46EA-B86D-CC498B7EDCC7','x64'=>'98783C40-AD89-4D1B-8668-72327F523C6B'},
'Product3'=>{'Win32'=>'04C7796A-2798-460C-B208-A0878566D75C','x64'=>'D8572936-A6F9-4E59-97A2-6C20EDA4B6EF'},
'Product4'=>{'Win32'=>'D114A35C-C763-4B8D-8D7E-F50C947A731C','x64'=>'F3F26709-AB84-49BB-9C72-57C37F241A85'}}[$flavour][$platform] %>"
'Product1'=>'8F675BC1-832C-48A6-8692-ABD5A8DBAA38',
'Product2'=>'A980A0EA-8B3E-48E4-B0E1-37270999FEDD'}[$flavour] :
Guid="<%= {'Product1'=>'03677B8C-EA6F-4C6C-9469-533BAF33EB25',
'Product2'=>'0F2E040C-F170-4025-A0D1-FAFF1244CEB8',
'Product3'=>'0C7EAD83-9D1D-470E-B61E-F4A00B794FB3',
'Product4'=>'19078A1B-6733-4F9E-BA9D-7E9B8B295DA6'}[$flavour] %>"
你就可以直接使用如下函数来实现:
1, 将以上内容赋值到oldFileContent参数.
2, 将keyword设为"Product2".
3, 调用RemoveSpecialString().
private string RemoveSpecialString(string oldFileContent, string keyword)
{
string guidPattern = "[?a-zA-Z0-9]{8}-[?a-zA-Z0-9]{4}-[?a-zA-Z0-9]{4}-[?a-zA-Z0-9]{4}-[?a-zA-Z0-9]{12}" ;
string removeStringPattern = @",/s*" + keyword +"=>{'Win32'=>'" + guidPattern + "','x64'=>'" + guidPattern + "'}";
oldFileContent = RemoveString(oldFileContent, removeStringPattern);
removeStringPattern = @"/s*" + keyword + "=>'" + guidPattern + "',";
oldFileContent = RemoveString(oldFileContent, removeStringPattern);
removeStringPattern = @",/s*" + keyword + "=>'" + guidPattern + "'";
oldFileContent = RemoveString(oldFileContent, removeStringPattern);
string newfileContent= RemoveString(oldFileContent, removeStringPattern);
return newfileContent;
}
private string RemoveString(string oldFileContent, string pattern)
{
return Regex.Replace(oldFileContent, pattern, string.Empty);
}