使用Reg-ex预解析文件以删除注释之间的文本

时间:2014-02-03 20:50:32

标签: c# regex

我有一个JSON文件,但有时它包含注释。为了反序列化它,我必须预先解析它以删除注释之间的任何内容。到目前为止,我已经制作了这个注册表,它没有删除不需要的文本。

StreamReader streamReader = new StreamReader(fileName);
string text = streamReader.ReadToEnd();
streamReader.Close();

string strRegex = @"\/\*.*|.*(\n\r)*\*\/";
RegexOptions myRegexOptions = RegexOptions.Multiline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
myRegex.Replace(text, "");

在下文中,我需要忽略注释之间的内容,例如从以下数据中,我的reg-ex应该只返回包含值Carieer S018的最后一个节点。

示例数据

{
    "Orders": [     

/*******************CALIBRATORS*********************/   
        /*{
            "Carrier": "S018",
            "SampleType": "Calibrator",
            "Position": 1,
            "CalMaterialLotNumber": "31032UI00",
            "CalMaterialExpirationDate": "07-07-2014",
            "AssayNumber": 241,
            "AssayVersion": 29,
            "Dilution": 1,
            "Replicate": 2,
            "MasterLotNumber": "31914UI00",
            "PackSerialNumber": "00001",
            "Comment": "TSH Cal",
        },


        {
            "Carrier": "S005",
            "SampleType": "Calibrator",
            "Position": 1,
            "CalMaterialLotNumber": "31032UI00",
            "CalMaterialExpirationDate": "07-07-2014",
            "AssayNumber": 696,
            "AssayVersion": 1,
            "Dilution": 1,
            "Replicate": 2,
            "MasterLotNumber": "89000UN13",
            "PackSerialNumber": "10001",
            "Comment": "Troponin Cal",
        },


                {
            "Carrier": "G008",
            "SampleType": "Calibrator",
            "Position": 1,
            "CalMaterialLotNumber": "31032UI00",
            "CalMaterialExpirationDate": "07-07-2014",
            "AssayNumber": 685,
            "AssayVersion": 1,
            "Dilution": 1,
            "Replicate": 2,
            "MasterLotNumber": "32916UI00",
            "PackSerialNumber": "50001",
            "Comment": "Folate Cal",
        },*/    


/*********************CONTROLS**************************/   




/*********************SAMPLES**************************/




/*************CARRIER 1****************/
    /*************C: S018 P: 1*************/
    {
        "Carrier": "S018",
        "SampleType": "Specimen",
        "SID": "1",
        "Position": 1,
        "AssayNumber": 241,
        "AssayVersion": 29,
        "Dilution": 1,
        "Replicate": 4,
        "Comment": "Pool",
    },

2 个答案:

答案 0 :(得分:1)

试试这个:

string strRegex = @"\/\*.*\*\/";
RegexOptions myRegexOptions = RegexOptions.Singleline;

请注意RegexOptions.Singleline。 'RegexOptions.Singleline'选项将整个输入字符串解释为单行,其中.(点)匹配输入字符串中的每个字符,包括\n(换行符)。

工作正则表达式示例:

http://regex101.com/r/eC3nW5

答案 1 :(得分:1)

MElliott几乎拥有它,但你需要通过添加.*使贪婪?懒惰:

string strRegex = @"\/\*.*?\*\/";

工作代码示例:

string text = @"{
    ""Orders"": [     

/*******************CALIBRATORS*********************/   
        {
            ""Carrier"": ""S018"",
            ""SampleType"": ""Calibrator"",
            ""Position"": 1,
            ""CalMaterialLotNumber"": ""31032UI00"",
            ""CalMaterialExpirationDate"": ""07-07-2014"",
            ""AssayNumber"": 241,
            ""AssayVersion"": 29,
            ""Dilution"": 1,
            ""Replicate"": 2,
            ""MasterLotNumber"": ""31914UI00"",
            ""PackSerialNumber"": ""00001"",
            ""Comment"": ""TSH Cal"",
        },

/*******************CALIBRATORS*********************/   

        {
            ""Carrier"": ""S005"",
            ""SampleType"": ""Calibrator"",
            ""Position"": 1,
            ""CalMaterialLotNumber"": ""31032UI00"",
            ""CalMaterialExpirationDate"": ""07-07-2014"",
            ""AssayNumber"": 696,
            ""AssayVersion"": 1,
            ""Dilution"": 1,
            ""Replicate"": 2,
            ""MasterLotNumber"": ""89000UN13"",
            /*""PackSerialNumber"": ""10001"",
            ""Comment"": ""Troponin Cal"",*/
        }
        ]}";

            string strRegex = @"\/\*.*?\*\/";
            RegexOptions myRegexOptions = RegexOptions.Singleline;
            Regex myRegex = new Regex(strRegex, myRegexOptions);
            text = myRegex.Replace(text, "");