Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用正则表达式-或任何其他解决方案-将编号(1-)(2-)更改为<;ul>&书信电报;李>;标签_C#_Regex - Fatal编程技术网

C# 使用正则表达式-或任何其他解决方案-将编号(1-)(2-)更改为<;ul>&书信电报;李>;标签

C# 使用正则表达式-或任何其他解决方案-将编号(1-)(2-)更改为<;ul>&书信电报;李>;标签,c#,regex,C#,Regex,我在C#中使用正则表达式时遇到了问题,或者至少我认为它可以解决我的问题,例如,我有以下字符串: “转到原始预算屏幕,\n1-检查原始预算是否为原始预算CV\n-检查剩余预算是否为剩余预算CV\n-检查新字段汇率\n-检查新字段原始预算FC\n-检查新字段剩余预算FC\n2-检查每周详细信息下重命名的字段:\n-检查流入是否为流出CV\n-检查流出是否为流出CV\n-检查新建流FC中的字段\n-签出新字段“流FC” 我需要它是: 转到原始预算屏幕检查原始预算是否正确 原始预算CV检查剩余预算是否剩

我在C#中使用正则表达式时遇到了问题,或者至少我认为它可以解决我的问题,例如,我有以下字符串:

“转到原始预算屏幕,\n1-检查原始预算是否为原始预算CV\n-检查剩余预算是否为剩余预算CV\n-检查新字段汇率\n-检查新字段原始预算FC\n-检查新字段剩余预算FC\n2-检查每周详细信息下重命名的字段:\n-检查流入是否为流出CV\n-检查流出是否为流出CV\n-检查新建流FC中的字段\n-签出新字段“流FC”

我需要它是:

转到原始预算屏幕检查原始预算是否正确 原始预算CV检查剩余预算是否剩余 预算CV检查新字段汇率检查新字段 字段原始预算FC
  • 检查新字段剩余预算 FC
  • 检查每周详细信息下的重命名字段:
    • 检查输入流是否在流CV中
    • 检查输出流是否在 流出流量CV检查流量FC中的新字段 磁场输出流量FC

    因此,如果这个简单的例子没有得到消息,我想把
    标记放在这个字符串中,这样我就可以在web应用程序上使用它了

    这是我的代码的一个简单示例:

      private string regexmethod(string field)
        {
            string strRegex = @"^(.+?)\\n[\d|\w]-\W+";
            Regex CatchBetweenTheFirstDot = new Regex(@"\\n[\d|\w]-\W+(.+?)\\n[\d|\w]-\W+");
            Regex CatchTheNumbers = new Regex(@"\\n[\d|\w]-\W+");
            Regex CatchFirstLineBeforDot = new Regex(strRegex);
            Regex CatchInsideFirstRegex = new Regex(@"\\n-\W+");
            var matchFirst = CatchFirstLineBeforDot.Matches(field);
            var matchBetween = CatchBetweenTheFirstDot.Matches(field);
            StringBuilder str = new StringBuilder();
            if (matchFirst.Count > 0)
                str.Append(matchFirst[0].Value);
            foreach (Match matchItem in matchBetween)
            {
                str.Append("<ul><li>");
                string newvalue = matchItem.Value;
                var matchTheInside = CatchInsideFirstRegex.Matches(newvalue);
                for (int i = 0; i < matchTheInside.Count; i++)
                {
                    if (i == 0)
                    {
                        newvalue.Replace(matchTheInside[i].Value, @"<ul><li>");
                    }
                    else
                    {
                        newvalue.Replace(matchTheInside[i].Value, @"<\li><li>");
                    }
                }
                if (newvalue.Length > 0)
                {
                    newvalue += "</li></ul>";
                    str.Append(newvalue);
                }
    
            }
    
    private string regexmethod(字符串字段)
    {
    字符串stregex=@“^(+?)\\n[\d|\w]-\w+”;
    Regex CatchBetweenTheFirstDot=新的正则表达式(@“\\n[\d|\w]-\w+(.+?)\\n[\d|\w]-\w+”;
    正则表达式CatchTheNumbers=新正则表达式(@“\\n[\d|\w]-\w+”;
    正则表达式CatchFirstLineBeforDot=新正则表达式(stregex);
    正则表达式CatchInsideFirstRegex=新正则表达式(@“\\n-\W+”);
    var matchFirst=CatchFirstLineBeforDot.Matches(字段);
    var matchBetween=catchbetwenthefirstdot.Matches(字段);
    StringBuilder str=新的StringBuilder();
    如果(matchFirst.Count>0)
    str.Append(matchFirst[0].Value);
    foreach(matchBetween中的matchItem匹配项)
    {
    str.Append(“
    • ”); 字符串newvalue=matchItem.Value; var matchTheInside=CatchInsideFirstRegex.Matches(newvalue); for(int i=0;i
    • ”; } 其他的 { newvalue.Replace(匹配内部[i].Value,@“
    • ”; } } 如果(newvalue.Length>0) { newvalue+=“
    ”; str.Append(newvalue); } }
    到目前为止,这是我所得到的,但对我来说不起作用


    任何帮助都将不胜感激。

    以下是解析文本行的课程:

    public class ListItem
    {
        public static bool TryParse(string s, out ListItem item)
        {
            item = null;
            if (!IsListItem(s) && !IsOrderedListItem(s))
                return false;
    
            item = new ListItem { IsOrdered = IsOrderedListItem(s),
                                  Text = GetItemText(s) };
            return true;
        }
    
        private static bool IsListItem(string s) {
            return Regex.IsMatch(s, @"^- .*");
        }
    
        private static bool IsOrderedListItem(string s) {                
            return Regex.IsMatch(s, @"^\d+- .*");
        }
    
        private static string GetItemText(string s) {
            return s.Substring(s.IndexOf('-') + 1).Trim();
        }
    
        public bool IsOrdered { get; private set; }
        public int Index { get; private set; }
        public string Text { get; private set; }
        public string Html {
            get { return String.Format("<li>{0}</li>", Text); }
        }
    }
    
    示例文本的输出:

    Go to Original Budget screen,
    <ul>
    <li>Check if Original Budget is Original Budget CV</li>
    <ul>
    <li>Check if Remaining Budget is Remaining Budget CV</li>
    <li>Check new field Exchange rate</li>
    <li>Check new field Original Budget FC</li>
    <li>Check new field Remaining Budget FC</li>
    </ul>
    <li>Check renamed field under Weekly Details:</li>
    <ul>
    <li>Check if In Flow is In Flow CV</li>
    <li>Check if Out Flow is Out Flow CV</li>
    <li>Check new field In Flow FC</li>
    <li>Check new field Out Flow FC</li>
    </ul>
    </ul>
    
    转到原始预算屏幕,
    
    • 检查原始预算是否为原始预算CV
      • 检查剩余预算是否为剩余预算CV
      • 检查新字段的汇率
      • 检查新字段原始预算FC
      • 检查新字段剩余预算FC
    • 选中“每周详细信息”下的重命名字段:
      • 检查输入流量是否在流量CV中
      • 检查流出流量是否为流出CV
      • 检查Flow FC中的新字段
      • 检查新的字段输出流FC

    您输入的预期输出是什么?那些
    Lorem Ipsum
    没有任何意义。如下列表和子列表:转到原始预算屏幕
    • 检查原始预算是否为原始预算CV
      • 检查剩余预算是否为剩余预算CV
      • 检查新字段汇率field Original Budget FC
      • 检查新字段剩余预算FC
      • 2-检查每周详细信息下的重命名字段:
        • 检查输入流是否在流CV中
        • 检查输出流是否在流CV中
        • 检查流FC中的新字段
        • 检查新字段输出流FC
          • 此列表的数据源是什么?有这是一种更简单的方法。文本来源于excel数据表。我使用了“Microsoft.Office.Interop.excel”",我从单元格中提取文本,但它没有将其提取为html样式。因此,我需要对其进行更改,以便将其上载到网站,excel电子表格从何处获取数据?也许您也需要解析html。首先,谢谢!第二,我尝试了,它可以工作,但这是输出转到原始预算屏幕,\n
              \n
            • 检查如果原始预算为原始预算CV
            • \n
                \n
              • 检查剩余预算是否为剩余预算CV
              • \n
              • 检查新字段汇率
              • \n
              • 检查新字段原始预算FC
              • \n
              • 检查新字段剩余预算FC
              • \n
              • 检查每周详细信息下的重命名字段:
              • \n
                  \n
                • 检查流入是否为流出CV
                • \n
                • 检查Out Flow是否为Out Flow CV
                • \n
                • 检查Flow FC中的新字段
                • \n
                • 检查Out Flow FC中的新字段
                • \n
                @JohnSmith如果您不需要与
                \n
                连接的行,那么只需使用空字符串进行连接:
                字符串。连接(“,CreateHtmlLists(行))
                。我只是使用了新行字符作为示例
                var lines = text.Split('\n');
                text = String.Join("\n", CreateHtmlLists(lines));
                
                Go to Original Budget screen,
                <ul>
                <li>Check if Original Budget is Original Budget CV</li>
                <ul>
                <li>Check if Remaining Budget is Remaining Budget CV</li>
                <li>Check new field Exchange rate</li>
                <li>Check new field Original Budget FC</li>
                <li>Check new field Remaining Budget FC</li>
                </ul>
                <li>Check renamed field under Weekly Details:</li>
                <ul>
                <li>Check if In Flow is In Flow CV</li>
                <li>Check if Out Flow is Out Flow CV</li>
                <li>Check new field In Flow FC</li>
                <li>Check new field Out Flow FC</li>
                </ul>
                </ul>