请问下面的代码怎么用正则表达式提取?
=?GB2312?B?suLK1NK7z8K/tL+0v8nE3MrVz8LAtA==?=
和
=?gb2312?Q?=B7=A2=D3=CA=BC=FE=C0=B2?=
怎么用语句出来编码与内容
我用的是下面这个,但中间的那个B是不确定的,而有可能是其它的字符如Q
=\\?(.+?)\\?B\\?(.+?)\\?=
而且后面
=B7=A2=D3=CA=BC=FE=C0=B2?=
到底是怎么编码的?我用gb2312无法得到呀?
我的代码如下:
System.Text.RegularExpressions.MatchCollection mc;
string strOriginalSubject;
string strSubject1 = string.Empty;
string strSubject = string.Empty;
string strChatset = string.Empty;
int iChatsetStartIndex;
int iSubjectStartIndex;
//@ "=?GB2312?B?suLK1NK7z8K/tL+0v8nE3MrVz8LAtA==?= "
//=?gb2312?Q?=B7=A2=D3=CA=BC=FE=C0=B2?=
strOriginalSubject = @ "=?GB2312?B?suLK1NK7z8K/tL+0v8nE3MrVz8LAtA==?= ";
mc = System.Text.RegularExpressions.Regex.Matches(strOriginalSubject, "=\\?(.+?)\\?B\\?(.+?)\\?= ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);// "=\\?(.+?)\\?B\\?(.+?)\\?= "
foreach (System.Text.RegularExpressions.Match m in mc)
{
strChatset = m.Groups[1].Value;//编码
strSubject1 = m.Groups[2].Value;//标题
if (strChatset != string.Empty)
{
//对主题进行解码
byte[] bSubject = System.Convert.FromBase64String(strSubject1);
strSubject = System.Text.Encoding.GetEncoding(strChatset).GetString(bSubject);
//需要连同编码规则一起替换掉
iChatsetStartIndex = strOriginalSubject.IndexOf( "=? " + strChatset + "? ");
if (iChatsetStartIndex < 0)
continue;
iSubjectStartIndex = strOriginalSubject.IndexOf( "? " + strSubject1 + "?= ", iChatsetStartIndex);
if (iSubjectStartIndex < 0)
continue;
strSubject1 = strOriginalSubject.Substring(iChatsetStartIndex, strChatset.Length + strSubject1.Length + 7);
strOriginalSubject = strOriginalSubject.Replace(strSubject1, strSubject);
}
}
MessageBox.Show(strOriginalSubject);
[解决办法]
不太清楚楼主的意思,看看这样是不是你想要的吧
string yourStr = ..........;
Match m = Regex.Match(yourStr, @ "=\?([^\?]*?)\?([^\?]*?)\?(.*?)\?= ");
if (m.Success)
{
richTextBox2.Text += m.Groups[1].Value + "\n ";
richTextBox2.Text += m.Groups[2].Value + "\n ";
richTextBox2.Text += m.Groups[3].Value + "\n ";
}
输出分别为
string yourStr = "=?GB2312?B?suLK1NK7z8K/tL+0v8nE3MrVz8LAtA==?= ";
GB2312
B
suLK1NK7z8K/tL+0v8nE3MrVz8LAtA==
string yourStr = "=?gb2312?Q?=B7=A2=D3=CA=BC=FE=C0=B2?= ";
gb2312
Q
=B7=A2=D3=CA=BC=FE=C0=B2
[解决办法]
string str = "?GB2312?B?suLK1NK7z8K/tL+0v8nE3MrVz8LAtA==?= ";
string pat = @ "\?(\w+)\?(\w)\?([\w/+=?]+) ";
Match m = Regex.Match(str, pat, RegexOptions.IgnoreCase);
strChatset.Text = m.Groups[1].Value;
bq.Text = m.Groups[2].Value;
strSubject1.Text = m.Groups[3].Value;