Solved

Regular Expression needed.

Posted on 2011-03-02
8
932 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

791 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