Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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
正则表达式帮助-从JavaScript转换为C#_C#_Regex - Fatal编程技术网

正则表达式帮助-从JavaScript转换为C#

正则表达式帮助-从JavaScript转换为C#,c#,regex,C#,Regex,我有一个字符串,我需要进行多次搜索和替换,以删除属性中的前导空格和尾随空格。这里显示了前后效果(视觉效果和JS示例): 现在,我需要在C#中执行等效操作-替换字符串中的所有引用。但我真的被卡住了。我知道模式是正确的,如JS版本所示,但是语法/转义语法让我头疼 这是我所拥有的,但它当然不起作用;-) //定义字符串 string xmlString=“某种类型的值另一种类型的值”; //这里是regexpatern-语法检查器根本不喜欢这样 字符串regexpatern=“/(specifica

我有一个字符串,我需要进行多次搜索和替换,以删除属性中的前导空格和尾随空格。这里显示了前后效果(视觉效果和JS示例):

现在,我需要在C#中执行等效操作-替换字符串中的所有引用。但我真的被卡住了。我知道模式是正确的,如JS版本所示,但是语法/转义语法让我头疼

这是我所拥有的,但它当然不起作用;-)

//定义字符串
string xmlString=“某种类型的值另一种类型的值”;
//这里是regexpatern-语法检查器根本不喜欢这样
字符串regexpatern=“/(specificatribute=)”\s*([^“]+?)\s*“/g”;
//这是替代品
字符串替换=“$1\”$2\”;
Regex rgx=新的Regex(regexpatern);
字符串结果=rgx.Replace(xmlString,replacement);
有人能告诉我我的错误吗


非常感谢!

删除regexpatern末尾的/g。这是我所看到的第一个错误。NET的regex实现没有全局修饰符,默认情况下是全局的

更新:

我认为这应该奏效:

           //define the string
            string xmlString = "<xml><elementName specificattribute=\" 111 222 333333 \" anotherattribute=\"something\" somethingelse=\"winkle\"><someotherelement>value of some kind</someotherelement><yetanotherelement>another value of some kind</yetanotherelement></elementName></xml>";

            // here's the regExPattern - the syntax checker doesn't like this at all
            string regExPattern = "(specificattribute=)\"\\s*([^\"]+?)\\s*";

            // here's the replacement
            string replacement = "$1\"$2\"";

            Regex rgx = new Regex(regExPattern);
            string result = rgx.Replace(xmlString, replacement);
//定义字符串
string xmlString=“某种类型的值另一种类型的值”;
//这里是regexpatern-语法检查器根本不喜欢这样
字符串regexpatern=“(specificattribute=)\”\\s*([^\“]+?)\\s*”;
//这是替代品
字符串替换=“$1\”$2\”;
Regex rgx=新的Regex(regexpatern);
字符串结果=rgx.Replace(xmlString,replacement);
尽管这实际上可能对您有用,但XML的嵌套/特定于上下文的特性使得正则表达式不适合正确有效地解析它

从外观上看,您应该真正使用Xpath或Linq to XML之类的东西来解析和修改这些属性

我实际上是在窃取Mark Byer的答案,但由于他的示例是使用xml文件的,并且您在内存中执行此操作,因此应该更像这样:

XDocument doc = XDocument.Parse("<xml><elementName specificattribute=\" 111 222 333333 \" anotherattribute=\"something\" somethingelse=\"winkle\"><someotherelement>value of some kind</someotherelement><yetanotherelement>another value of some kind</yetanotherelement></elementName></xml>");
foreach (XAttribute attr in doc.Descendants("elementName")
                               .Attributes("specificattribute"))
{
    attr.Value = attr.Value.Trim();
}
string result = doc.ToString();
XDocument doc=XDocument.Parse(“某种类型的值另一种类型的值”);
foreach(文档子体中的XAttribute attr(“elementName”)
.Attributes(“specificatribute”))
{
attr.Value=attr.Value.Trim();
}
字符串结果=doc.ToString();

此任务不使用正则表达式..NET具有处理XML文档的强大工具。请尝试以下方法:

XDocument doc = XDocument.Load("input.xml");
foreach (XAttribute attr in doc.Descendants("elementName")
                               .Attributes("specificattribute"))
{
    attr.Value = attr.Value.Trim();
}
doc.Save("output.xml");

说真的,您应该为此使用System.Xml类。下面是另一个使用的示例:

string xmlString=“某种类型的值另一种类型的值”;
XmlDocument xml=新的XmlDocument();
LoadXml(xmlString);
foreach(xml.SelectNodes(“//@specificatribute”)中的XmlAttribute el)
{
el.Value=el.Value.Trim();
}

尝试在regexpatern字符串上插入@符号,如下所示:String regexpatern=@/(specificatribute=)“\s*([^“]+?)\s*”/g”;您不应该使用正则表达式来解析XML。C#拥有处理XML文档的强大工具。
XDocument doc = XDocument.Load("input.xml");
foreach (XAttribute attr in doc.Descendants("elementName")
                               .Attributes("specificattribute"))
{
    attr.Value = attr.Value.Trim();
}
doc.Save("output.xml");
    string xmlString = "<xml><elementName specificattribute=\" 111 222 333333 \" anotherattribute=\"something\" somethingelse=\"winkle\"><someotherelement>value of some kind</someotherelement><yetanotherelement>another value of some kind</yetanotherelement></elementName></xml>";

    XmlDocument xml = new XmlDocument(); ;
    xml.LoadXml(xmlString);

    foreach (XmlAttribute el in xml.SelectNodes("//@specificattribute"))
    {
        el.Value = el.Value.Trim();
    }