Regular Expression needed.

Hi,

I need a regular expression that extracts the filename.gif from a string like <img src="filename.gif" alt="alttext">

But it could also be reordered and with some whitespaces like

<img alt="alttext" src = "filename.gif"/>

Thanks in advance
LVL 12
HugoHiaslAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Peter KwanConnect With a Mentor Commented:
Or even simplier:

<img (?:\S+ )*src\s*=\s*"([^"]+)"(?: \S+)*

Open in new window

0
 
Peter KwanCommented:
Please try this one.

<img (?:\S+ )*src\s*=\s*"([^"]+)"(?: \S+)*/>

Open in new window

0
 
dds_fellesConnect With a Mentor Commented:
I always use RegEx Coach to create / test / verify regular expressions.

I'm pretty sure is freeware;

http://weitz.de/regex-coach/

Good luck ;-)
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
HugoHiaslAuthor Commented:
Thanks for the first responses.

I already have multiple <img src="filename.gif" alt=""> strings extracted from a html-page.

I need the get the filenames for the gifs.

In c# it shoult be a

string imageTagString = "<img src=\"filename.gif\" alt=\"\"/>";
MatchCollection matches = exp.Matches(imageTagString);

the matches[0] should be "filename.gif"

Best regards
0
 
Peter KwanCommented:
Please try this sample code:

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;

static class Program {
	static void Main(string[] args) {

		Regex exp=new Regex("<img (?:\\S+ )*src\\s*=\\s*\"([^\"]+)\"(?: \\S+)*");
		string imageTagString = "<img alt=\"\" src = \"filename.gif\"/>";
		MatchCollection matches = exp.Matches(imageTagString);
		foreach(Match m in matches)
		{
			 Console.WriteLine("Test " + m.Groups[1]);
		}
		
	}
}

Open in new window

0
 
HugoHiaslAuthor Commented:
I used this one:
            Regex rg = new Regex(@"<img.*>", RegexOptions.IgnoreCase);

            MatchCollection matchList = rg.Matches(message.Body.Text);

            foreach (Match match in matchList)
            {
                string imgString = match.Value;
                Regex rg2 = new Regex(@"<img (?:\S+ )*src\s*=\s*\""([^""]+)""(?: \S+)*");

                MatchCollection matchList2 = rg2.Matches(imgString);

            }

Open in new window


matchList[0] contains
<img src="IVQHeader.gif" alt="Header">


matchList2[0] contains the same.

This seems not to be the right RegEx for rg2.
0
 
HugoHiaslAuthor Commented:
I only want the filename of the image which is in the src="filename.gif" part of the tag.


It would also be ok if it needs a 2 step approach with 2 RegEx.
0
 
HugoHiaslAuthor Commented:
The tool is perfect. Thanks.

I managed to get what I needed. Thanks to all.
0
All Courses

From novice to tech pro — start learning today.