Solved

Regular Expression needed.

Posted on 2011-03-02
8
933 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:HugoHiasl
[X]
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
8 Comments
 
LVL 16

Expert Comment

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

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

Open in new window

0
 
LVL 16

Accepted Solution

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

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

Open in new window

0
 
LVL 1

Assisted Solution

by:dds_felles
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;

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

Good luck ;-)
0
Industry Leaders: 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!

 
LVL 12

Author Comment

by:HugoHiasl
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
0
 
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

0
 
LVL 12

Author Comment

by:HugoHiasl
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.
0
 
LVL 12

Author Comment

by:HugoHiasl
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.
0
 
LVL 12

Author Closing Comment

by:HugoHiasl
ID: 35016672
The tool is perfect. Thanks.

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

Featured Post

Technology Partners: 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…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

740 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