Click here to Skip to main content
15,904,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C#
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
Posted
Updated 15-May-16 22:06pm

A quick run of that code:
C#
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}
And the second isn't a match, so the foreach refuses to cast it.
I would go with:
C#
Match matchImgTags = Regex.Match(source, @"src\s*=\s*""([^""]+)", RegexOptions.IgnoreCase);
if (matchImgTags.Success)
    {
    string s = matchImgTags.Value;
    ...
    }

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:
C#
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);
    }



"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:
C#
string s1 = "hello \"user\" this is a string";

C#
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").+?( ...
Don't need any special processing.
Unless...you want to use a C# string as input to a regex:
C#
string s = @"... =src"").+?( ...";

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?
 
Share this answer
 
v2
Comments
Member 12525734 16-May-16 4:49am    
I got no match on regexr.com on:
(?<=src=").+?(?=})

on this text:

<img class="alignright size-medium wp-image-63" alt="016stor" src="http://www.mertidattleva.se/wp-content/uploads/2013/04/016stor-300x200.jpg" width="300" height="200" />Helt klart kommer denna charmiga tillbyggnad garantera att din sommar blir lite mysigare och lite skönare. Solen fortsätter som bekant att skina även när sommaren går mot sitt slut och sedan övergår i höst, lite beroende på vilken form av rutor du valt så fortsätter uterummet att alstra värme och den sköna känslan stannar kvar långt efter att temperaturen utanför sjunkit och höstkläderna tagits fram. Sedan, när vintern kommit och snön ligger vit utanför kan julgranen ställas upp i uterummet och julens alla ljus och all dess värme skiner ut i ett gnistrande vinterlandskap med bara iskristallskimrande glasväggar emellan.
OriginalGriff 16-May-16 5:00am    
That's because it's a different format to your first example - which ends with a "}"
You could change the end detection part to to:
(?=["}])
which would match either, but if you are trying to use a regex to generically scrape websites, it's probably going to give you problems all the time. You would be a lot better off using one of the many HTML based scrapers.
Member 12525734 16-May-16 5:05am    
I changed matchImgTags to

Match matchImgTags = Regex.Match(source, @"(?<=src="").+?(?=["}])", RegexOptions.IgnoreCase);

but got error in visual studio
OriginalGriff 16-May-16 5:18am    
:sigh:
Because the single double quote terminated the string?
You didn't think to change it to "" because you entered it in a string...
Member 12525734 16-May-16 5:34am    
How would the string be since i don't have a } at the end ?
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>[^\"]+)

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



Get your self some good Regex tools and learn how to use regular expressions.

This is a pretty good site:
Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns[^]

And this is a pretty good and simple tool:
RegExTest download | SourceForge.net[^]
 
Share this answer
 
v2
You are passing an URL, not a proper file path to Path.GetFileName().

Try the following:

C#
var address = new Uri(match.Value);
var path = address.AbsolutePath;
var fileName = Path.GetFileName(path);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900