【问题标题】:How to parse this .js file into an xml file using regex in c#?如何在 C# 中使用正则表达式将此 .js 文件解析为 xml 文件?
【发布时间】:2016-11-06 05:55:01
【问题描述】:
//testing.js file
describe('Criteria and Adjustment Section', function () {
    it('the labels should have correct spellings -Expected result- the labels have correct spellings', function () {
    //some logic
});


describe('Test 1', function () {
    it('Click on the company dropdown -Expected result- Four options will be shown', function () {
    //some logic 
});

//有多个这样的描述函数。

//这是一个 testing.js 文件,我必须将此文件解析为一个 xml 文件,使其看起来像:

//.xml file
Test No.     Test-Cases                 Expected Results
----------
01           all the labels should      the labels have correct spellings
             have correct spellings

----------
02           Click on the company       Four options will be shown
             dropdown

【问题讨论】:

  • 输入是Javascript代码,你显示的输出不是XML。目前尚不清楚您要问什么,无论如何您都需要展示您编写的代码并提出特定问题。这里没有人会为你编写代码。
  • 你说的是从 mocha 测试生成 xml 单元测试报告吗? github.com/michaelleeallen/mocha-junit-reporter

标签: c# regex xml string-parsing


【解决方案1】:

你可以尝试的正则表达式是:

describe\('[^']*', function \(\) {.*?it\('([^']*)-Expected result-([^']*)',

Explanation

示例 C# 代码:

using System;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main()
    {

        string pattern = @"describe\('[^']*', function \(\) {.*?it\('([^']*)-Expected result-([^']*)',";
        string input = @"describe('Criteria and Adjustment Section', function () {
    it('the labels should have correct spellings -Expected result- the labels have correct spellings', function () {
    //some logic
});


describe('Test 1', function () {
    it('Click on the company dropdown -Expected result- Four options will be shown', function () {
    //some logic 
});";
        RegexOptions options = RegexOptions.Multiline | RegexOptions.Singleline;
        int count=0;
        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            ++count;
            Console.WriteLine("Test No : "+count);
            Console.WriteLine("Test-Cases: "+m.Groups[1].Value);
            Console.WriteLine("Expected Reult: "+m.Groups[2].Value);

        }
    }
}

样本输出

Test No : 1
Test-Cases: the labels should have correct spellings 
Expected Reult:  the labels have correct spellings
Test No : 2
Test-Cases: Click on the company dropdown 
Expected Reult:  Four options will be shown

Run the code here

根据需要格式化输出

【讨论】:

  • 非常感谢小牛!我遇到的问题主要是正则表达式模式。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
  • 2014-05-16
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多