Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.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# 解析C语言中嵌套的CSS样式文本#_C#_Css_String Parsing_Css Parsing - Fatal编程技术网

C# 解析C语言中嵌套的CSS样式文本#

C# 解析C语言中嵌套的CSS样式文本#,c#,css,string-parsing,css-parsing,C#,Css,String Parsing,Css Parsing,我希望有一个文本作为C#中的字符串输入,如处理前的标题下所示。此文本的格式应确保: 没有任何样式标记的裸句(如句子1)必须有一个样式标记,以使整个句子变为粗体 需要识别已经具有样式标记的句子,并且必须将其前景色元素设置为“fg:Red”,以便使整个句子看起来是红色的 已经有样式标记的句子可能有嵌套的样式标记。因此,这需要加以考虑 例如,格式化完成后,处理前标题中的句子应与处理后标题下的文本类似 我的问题是,用C#实现这种文本处理业务最有效的方法是什么?是使用正则表达式还是过度使用?你认为会有更好

我希望有一个文本作为C#中的字符串输入,如处理前的标题下所示。此文本的格式应确保:

  • 没有任何样式标记的裸句(如句子1)必须有一个样式标记,以使整个句子变为粗体
  • 需要识别已经具有样式标记的句子,并且必须将其前景色元素设置为“fg:Red”,以便使整个句子看起来是红色的
  • 已经有样式标记的句子可能有嵌套的样式标记。因此,这需要加以考虑 例如,格式化完成后,处理前标题中的句子应与处理后标题下的文本类似

    我的问题是,用C#实现这种文本处理业务最有效的方法是什么?是使用正则表达式还是过度使用?你认为会有更好的选择吗?谢谢

    (我正在使用C#4)

    处理前

    "Sentence 1 <style styles='B;fg:Green'>STYLED SENTENCE</style> Sentence 2"
    
    "<style styles='B'>Sentence 1 </style> 
     <style styles='B;fg:Red'>STYLED  SENTENCE</style>  
     <style styles='B'>Sentence 2</style>"
    
    “第1句风格的第2句”
    
    处理后

    "Sentence 1 <style styles='B;fg:Green'>STYLED SENTENCE</style> Sentence 2"
    
    "<style styles='B'>Sentence 1 </style> 
     <style styles='B;fg:Red'>STYLED  SENTENCE</style>  
     <style styles='B'>Sentence 2</style>"
    
    “第1句
    风格句
    第2句“
    
    您可以尝试以下基于正则表达式的解决方案:

    string myLine = "Sentence 1<style styles='B;fg:Green'>STYLED SENTENCE</style>Sentence 2";
    const string splitLinesRegex = @"((?<Styled>\<style[^\>]*\>[^\<\>]*\</style\>)|(?<NoStyle>[^\<\>]*))";
    
    var splitLinesMatch = Regex.Matches(myLine, splitLinesRegex, RegexOptions.Compiled);
    List<string> styledLinesBis = new List<string>();
    
    foreach (Match item in splitLinesMatch)
    {
        if (item.Length > 0)
        {
            if (!string.IsNullOrEmpty(item.Groups["Styled"].Value))
                styledLinesBis.Add(string.Format("<style styles='B'>{0}</style> ", item.Groups["Styled"].Value));
    
            if (!string.IsNullOrEmpty(item.Groups["NoStyle"].Value))
                styledLinesBis.Add(string.Format("<style styles='B;fg:Red'>{0}</style>  ", item.Groups["NoStyle"].Value));
        }
    }
    
    string myLine=“句子1风格句子句子2”;
    常量字符串splitLinesRegex=@“((?\]*\>[^\]*\)\(?[^\]*])”;
    var splitLinesMatch=Regex.Matches(myLine、splitLinesRegex、RegexOptions.Compiled);
    List styledLinesBis=新列表();
    foreach(splitLinesMatch中的匹配项)
    {
    如果(item.Length>0)
    {
    如果(!string.IsNullOrEmpty(item.Groups[“Styled”].Value))
    添加(string.Format({0}),item.Groups[“Styled”].Value));
    如果(!string.IsNullOrEmpty(item.Groups[“NoStyle”].Value))
    添加(string.Format({0}),item.Groups[“NoStyle”].Value));
    }
    }
    

    您只需使用string.join语句来连接字符串。

    您可以尝试以下基于正则表达式的解决方案:

    string myLine = "Sentence 1<style styles='B;fg:Green'>STYLED SENTENCE</style>Sentence 2";
    const string splitLinesRegex = @"((?<Styled>\<style[^\>]*\>[^\<\>]*\</style\>)|(?<NoStyle>[^\<\>]*))";
    
    var splitLinesMatch = Regex.Matches(myLine, splitLinesRegex, RegexOptions.Compiled);
    List<string> styledLinesBis = new List<string>();
    
    foreach (Match item in splitLinesMatch)
    {
        if (item.Length > 0)
        {
            if (!string.IsNullOrEmpty(item.Groups["Styled"].Value))
                styledLinesBis.Add(string.Format("<style styles='B'>{0}</style> ", item.Groups["Styled"].Value));
    
            if (!string.IsNullOrEmpty(item.Groups["NoStyle"].Value))
                styledLinesBis.Add(string.Format("<style styles='B;fg:Red'>{0}</style>  ", item.Groups["NoStyle"].Value));
        }
    }
    
    string myLine=“句子1风格句子句子2”;
    常量字符串splitLinesRegex=@“((?\]*\>[^\]*\)\(?[^\]*])”;
    var splitLinesMatch=Regex.Matches(myLine、splitLinesRegex、RegexOptions.Compiled);
    List styledLinesBis=新列表();
    foreach(splitLinesMatch中的匹配项)
    {
    如果(item.Length>0)
    {
    如果(!string.IsNullOrEmpty(item.Groups[“Styled”].Value))
    添加(string.Format({0}),item.Groups[“Styled”].Value));
    如果(!string.IsNullOrEmpty(item.Groups[“NoStyle”].Value))
    添加(string.Format({0}),item.Groups[“NoStyle”].Value));
    }
    }
    

    您只需使用string.join语句来连接字符串。

    您可以使用正则表达式来连接字符串。“**”是换行顺序吗?很抱歉,这些星星是多余的,是错误添加的。我已经把它们移走了。你能用第一句话和第二句话吗?句子的结尾字符是什么?或者我们可以认为标签是句子分隔符吗?没有特定的结束字符。这是自然语言的自由文本。我们应该继续处理,直到到达文本的结尾。是的,请确保样式标记是句子分隔符…您可以使用正则表达式进行标记。“**”是换行顺序吗?很抱歉,这些星星是多余的,是错误添加的。我已经把它们移走了。你能用第一句话和第二句话吗?句子的结尾字符是什么?或者我们可以认为标签是句子分隔符吗?没有特定的结束字符。这是自然语言的自由文本。我们应该继续处理,直到到达文本的结尾。是的,确保样式标记是句子分隔符。。。