C#正则表达路径中的无效字符。 [英] C# regex invalid characters in the path.

查看:112
本文介绍了C#正则表达路径中的无效字符。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中出现错误

var fileName = Path.GetFileName(match.Value)





路径中的字符无效。



字符串为:



match = {src = http://www.mertidattleva.se/wp-content/uploads/2013/04/016stor-300x200.jpg}



怎么了?



I got error in this code in
var fileName = Path.GetFileName(match.Value)


Invalid characters in the path.

the string is:

match = {src="http://www.mertidattleva.se/wp-content/uploads/2013/04/016stor-300x200.jpg}

What's wrong?

Match matchImgTags = Regex.Match(source, @"src\s*=\s*""([^""]+)", RegexOptions.IgnoreCase);
                                    
                            if (matchImgTags.Groups.Count > 0)
                            {
                                foreach (Match match in matchImgTags.Groups)
                                {
                                    var fileName = Path.GetFileName(match.Value);

                                    source = source.Replace(match.Value, fileName);
                                }
                            }
                            else
                            {
                                var fileName = Path.GetFileName(matchImgTags.Groups[0].Value);
                                source = source.Replace(matchImgTags.Value, "");
                            }





我的尝试:



在互联网上搜索不匹配



What I have tried:

googled on internet no match there

推荐答案

快速运行该代码:

A quick run of that code:
string source = @"match = {src=""http://www.mertidattleva.se/wp-content/uploads/2013/04/016stor-300x200.jpg}";
Match matchImgTags = Regex.Match(source, @"src\s*=\s*""([^""]+)", RegexOptions.IgnoreCase);

if (matchImgTags.Groups.Count > 0)
    {
    foreach (Match match in matchImgTags.Groups)
        {
        var fileName = Path.GetFileName(match.Value);

        source = source.Replace(match.Value, fileName);
        }
    }
else
    {
    var fileName = Path.GetFileName(matchImgTags.Groups[0].Value);
    source = source.Replace(matchImgTags.Value, "");
    }



从输入字符串给我两个匹配:


Gives me two matches from the input string:

src="http://www.mertidattleva.se/wp-content/uploads/2013/04/016stor-300x200.jpg}

第二个不匹配,所以foreach拒绝投出它。 />
我会选择:

And the second isn't a match, so the foreach refuses to cast it.
I would go with:

Match matchImgTags = Regex.Match(source, @"src\s*=\s*""([^""]+)", RegexOptions.IgnoreCase);
if (matchImgTags.Success)
    {
    string s = matchImgTags.Value;
    ...
    }



但即便如此,返回的匹配字符串也不是有效的文件名,因此Path.GetFileName会抛出异常。

您需要做的是更改正则表达式:


But even then, the returned match string is not a valid filename, so Path.GetFileName throws an exception.
What you need to do is change the regex:

(?<=src=").+?(?=})

可能会这样做:

Will probably do it:

string source = @"match = {src=""http://www.mertidattleva.se/wp-content/uploads/2013/04/016stor-300x200.jpg}";
Match matchImgTags = Regex.Match(source, @"(?<=src="").+?(?=})", RegexOptions.IgnoreCase);
if (matchImgTags.Success)
    {
    string s = matchImgTags.Value;
    string fileName = Path.GetFileName(s);
    source = source.Replace(s, fileName);
    }







我是regex的初学者,下载浓缩咖啡放入此字符串:

?< = src =)。+?(?= [}])

但不知道是什么错误






:叹息:

这里有两个独立的系统:正则表达式和C#字符串。

输入C#字符串时,它以双引号开头和结尾:

string s =hello user this is a string;

如果你想在字符串中包含双引号,你有两种方法可以做到:




"I am a beginner to regex, downloaded espresso put in this string:
?<=src="").+?(?=["}])
but don't know what's wrong"



:sigh:
You have two separate systems here: regex and C# strings.
When you enter a C# string, it starts and ends with a double quote:
string s = "hello user this is a string";
If you want to include a double quote in the string, you have two ways to do it:

string s1 = "hello \"user\" this is a string";




string s2 = @"hello ""user"" this is a string";



但正则表达式不要将双引号视为特殊字符 - 他们使用反斜杠 - 所以包含双引号的正则表达式:


but Regexs don't regard double quote as a special character at all - they do with backslash - so a regexes that contains a double quote:

... =src").+?( ...

不需要任何特殊处理。

除非......你想使用C#字符串作为正则表达式的输入:

Don't need any special processing.
Unless...you want to use a C# string as input to a regex:

string s = @"... =src"").+?( ...";



因此,如果您使用C#字符串并将其粘贴到Expresso(处理正则表达式,而不是C#)字符串)你必须手动非特殊情况它并将双引号双引号压缩为单引号。就像你手动将它粘贴到Word,或记事本,或任何其他不能使用C#字符串的应用程序(即大多数!)一样好的时候。

现在有意义吗?


So if you take a C# string and you paste it into Expresso (which deals with regexes, not C# strings) you have to manually "un-special case" it and condense doubled double quotes to single double quotes. Exactly as if you manually pasted it into Word, or Notepad, or any other app that doesn't work with C# strings (i.e. most of them!)
Make sense now?


你没有转义引号()



表达式

You have not escaped the quotation mark (")

The expression
src\s*=\s*""([^""]+)



应该是


should be

src\s*=\s*\"([^\"]+)





如果你想要使用命名组,这有时会让生活更轻松,这样做



And if you want to use named groups, which makes life easier sometimes, do like this

src\s*=\s*\"(?<path>[^\"]+)




Match m = Regex.Match(source, @"src\s*=\s*\"(?<path>[^\"]+)", RegexOptions.IgnoreCase);
string path = m.Groups["path"].Value;







给自己一些好的正则表达式工具并学习如何使用正则表达式。



这是一个非常好的网站:

Regular-Expressions.info - 正则表达式教程,示例和参考 - 正则表达式模式 [ ^ ]



这是一个非常好的简单工具:

RegExTest下载| SourceForge.net [ ^ ]


您正在传递一个URL,而不是Path.GetFileName()的正确文件路径。



请尝试以下方法:



You are passing an URL, not a proper file path to Path.GetFileName().

Try the following:

var address = new Uri(match.Value);
var path = address.AbsolutePath;
var fileName = Path.GetFileName(path);


这篇关于C#正则表达路径中的无效字符。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆