正则表达式从给定字符串中提取编码类型

时间:2013-07-10 20:28:05

标签: c# regex

在stackoverflow中recent thread之后,我发布了一个新问题: 我有几个字符串,我想从中提取编码类型。 我愿意使用正则表达式来做到这一点:

示例:

utf-8 quoted printable
string str = "=?utf-8?Q?=48=69=67=68=2d=45=6e=64=2d=44=65=73=69=67=6e=65=72=2d=57=61=74=63=68=2d=52=65=70=6c=69=63=61=73=2d=53=61=76=65=2d=54=48=4f=55=53=41=4e=44=53=2d=32=30=31=32=2d=4d=6f=64=65=6c=73?=";

utf-8 Base 64
string fld4 = "=?utf-8?B?VmFsw6lyaWUgTWVqc25lcm93c2tp?= <Valerie.renamed@company.com>";

Windows 1258 Base 64
 string msg2= "=?windows-1258?B?UkU6IFRyIDogUGxhbiBkZSBjb250aW51aXTpIGQnYWN0aXZpdOkgZGVz?= =?windows-1258?B?IHNlcnZldXJzIFdlYiBHb1ZveWFnZXN=?=";

iso-8859-1 Quoted printable 
string fld2 = "=?iso-8859-1?Q?Fr=E9d=E9ric_Germain?= <Frederic.Germain@company.com>";

等...

为了编写通用解码功能,我们需要提取:

  • 字符集(utf-8,Windows1258等等)

  • transfert编码类型(引用可打印或基础64)

  • 编码字符串

知道如何在?xxx?Q之间提取模式?还是?xxx?B?

注意:这可以是大写或小写

感谢。

2 个答案:

答案 0 :(得分:2)

Here is a Rubular会为你做的。简而言之,此正则表达式=\?(.*?)\?[QBqb]将获取该编码。但有一点需要注意的是,当抓住结果时,你给出的第三个例子中有两个匹配,所以请确保你决定用第二场比赛做什么。

答案 1 :(得分:0)

这是一个完整的解决方案

public class Encoded
    {
        public string Charset;
        public string ContentTransfertEncoding;
        public string Data;
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;

    namespace ConsoleApplication2
    {
     public class Decoding
{

    public Decoding()
    {

    }

    public List<Encoded> Process(string data)
    {
        List<Encoded> list = new List<Encoded>();

        var occurences = new Regex(@"=\?[a-zA-Z0-9?=-]*\?[BbQq]\?[a-zA-Z0-9?=-]*\?=", RegexOptions.IgnoreCase);
        var matches = occurences.Matches(data);
        foreach (Match match in matches)
        {
            Encoded cls = new Encoded();
            cls.Data = match.Groups[0].Value;
            cls.Charset = GetCharset(cls.Data);                
            cls.ContentTransfertEncoding = GetContentTransfertEncoding(cls.Data);

            // cleanup data
            int pos = cls.Data.IndexOf("=?");
            pos = cls.Data.IndexOf("?",pos+ 2);
            cls.Data = cls.Data.Substring(pos + 3);
            cls.Data = cls.Data.Replace("?=", "");                
            list.Add(cls);
        }
        return list;            
    }

    private string GetContentTransfertEncoding(string data)
    {
        var occurences = new Regex(@"=\?(.*?)\?[QBqb]", RegexOptions.IgnoreCase);
        var matches = occurences.Matches(data);

        foreach (Match match in matches)
        {
            int pos = match.Groups[0].Value.LastIndexOf('?');

            return match.Groups[0].Value.Substring(pos+1);

        }
        return data;
    }

    public string GetCharset(string data)
    {
        var occurences = new Regex(@"=\?(.*?)\?[QBqb]", RegexOptions.IgnoreCase);
        var matches = occurences.Matches(data);

        foreach (Match match in matches)
        {
            string str1 = match.Groups[0].Value.Replace("=?", "");
            int pos = str1.IndexOf('?');
            str1 = str1.Substring(0, pos);
            return str1; // there should be only 1 match
        }
        return data;
    }

    public string Decodeetc...()
   }