如何使用c#中的正则表达式将这个.js文件解析为xml文件?
//有多种类似于此的描述it功能 //这是一个testing.js文件,我必须将该文件解析为xml文件,使其看起来像:如何使用c#中的正则表达式将这个.js文件解析为xml文件?,c#,regex,xml,string-parsing,C#,Regex,Xml,String Parsing,//有多种类似于此的描述it功能 //这是一个testing.js文件,我必须将该文件解析为xml文件,使其看起来像: //testing.js file describe('Criteria and Adjustment Section', function () { it('the labels should have correct spellings -Expected result- the labels have correct spellings', function ()
//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
});
您可以尝试的正则表达式是:
//.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
示例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);
}
}
}
根据需要格式化输出输入是Javascript代码,显示的输出不是XML。不清楚您在问什么,而且在任何情况下,您都需要显示您编写的代码并提出特定的问题。这里没有人会为您编写代码。您是说从mocha测试生成xml单元测试报告吗?谢谢你这么特立独行!我主要在正则表达式模式方面遇到了问题。:)
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