Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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#_.net_Regex - Fatal编程技术网

C# 替换文本字符串

C# 替换文本字符串,c#,.net,regex,C#,.net,Regex,我有一个包含大量文本的标记文件,如下所示: 我的标记文件和更多文本 还有另一个标题,胡说八道 如何用新标题替换所有这些标题(可能使用正则表达式)?首先有两条规则: 标题引号之间的值不同 还有其他与标题无关的value=“xxxxx”文本 谢谢 在不知道标题中的各种值的情况下……您可以使用支持正则表达式查找和替换的文本编辑器,如notepad++,使用: <title value="[A-Za-z ]*" /> 作为你的正则表达式。如果您的标题不仅仅包含数字字符和空格,您需要更

我有一个包含大量文本的标记文件,如下所示:

我的标记文件和更多文本
还有另一个标题,胡说八道
如何用新标题替换所有这些标题(可能使用正则表达式)?首先有两条规则:

  • 标题引号之间的值不同
  • 还有其他与标题无关的
    value=“xxxxx”
    文本

  • 谢谢

    在不知道标题中的各种值的情况下……您可以使用支持正则表达式查找和替换的文本编辑器,如notepad++,使用:

    <title value="[A-Za-z ]*" />
    
    
    

    作为你的正则表达式。如果您的标题不仅仅包含数字字符和空格,您需要更改[A-Za-Z]

    您认为这样做可以:

    string output = Regex.Replace(txt,"<title value=\".*?\"","<title value=\"My new title\"");
    

    string output=Regex.Replace(txt),如果要替换双引号内的某些文本,可以执行以下操作:

    string originalStr = "My markup file <title value=\"The title\" /> and more text and another title <title value=\"XXX\" />The <text> blah blah blah";
    
    string replacedStr = Regex.Replace(originalStr, @"""[^""]+""", "\"NewText\"");
    
    string originalStr=“我的标记文件和更多文本以及另一个标题,诸如此类”;
    字符串replacedStr=Regex.Replace(originalStr,@“[^”]+”,“\“NewText\”);
    
    您可以使用和实现以下功能:

    public static string ReplaceTitle(string input, string newTitle)
    {
        string findPattern = @"(?<prepend><title\s+value\s*=\s*\"")([^\""]*)(?<append>\"")";
        string replacePattern = "${prepend}" + newTitle + "${append}";
    
        return Regex.Replace(input, findPattern, replacePattern, RegexOptions.IgnoreCase);
    }
    
    publicstaticstringreplacettitle(字符串输入,字符串newTitle)
    {
    
    string findPattern=@”(?如果您的文件是XML(或HTML),请使用适当的解析器进行解析和替换。它是一个伪标记,因此不是纯XML。
    ?无法识别的转义序列的任何实例。请尝试
    string findPattern=@)(?np,它仍然有点不正确,现在它有了@\“是字符串分隔符,所以您需要\”现在应该可以了,谢谢!
    
    public static string ReplaceTitle(string input, string newTitle)
    {
        string findPattern = @"(?<prepend><title\s+value\s*=\s*\"")([^\""]*)(?<append>\"")";
        string replacePattern = "${prepend}" + newTitle + "${append}";
    
        return Regex.Replace(input, findPattern, replacePattern, RegexOptions.IgnoreCase);
    }
    
    using System.IO;
    using System;
    using System.Text.RegularExpressions;
    
    public class Program
    {
        static void Main()
        {
            string input = "My markup file <title value=\"The title\" /> and more text and another title <title value=\"XXX\" />The <text> blah blah blah";
    
            Console.WriteLine(ReplaceTitle(input, "NEWTITLE"));        
        }
    
        public static string ReplaceTitle(string input, string newTitle)
        {
            string findPattern = @"(?<prepend><title\s+value\s*=\s*\"")([^\""]*)(?<append>\"")";
            string replacePattern = "${prepend}" + newTitle + "${append}";
    
            return Regex.Replace(input, findPattern, replacePattern, RegexOptions.IgnoreCase);
        }
    }