Regular Expression needed.

Posted on 2011-03-02
Last Modified: 2012-05-11

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
Question by:HugoHiasl
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 16

Expert Comment

by:Peter Kwan
ID: 35015383
Please try this one.

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

Open in new window

LVL 16

Accepted Solution

Peter Kwan earned 125 total points
ID: 35015428
Or even simplier:

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

Open in new window


Assisted Solution

dds_felles earned 125 total points
ID: 35015640
I always use RegEx Coach to create / test / verify regular expressions.

I'm pretty sure is freeware;

Good luck ;-)
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 12

Author Comment

ID: 35015731
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
LVL 16

Expert Comment

by:Peter Kwan
ID: 35015866
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

LVL 12

Author Comment

ID: 35016502
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.
LVL 12

Author Comment

ID: 35016549
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.
LVL 12

Author Closing Comment

ID: 35016672
The tool is perfect. Thanks.

I managed to get what I needed. Thanks to all.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question