Solved

Regular Expression needed.

Posted on 2011-03-02
8
925 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 12

Author Comment

by:HugoHiasl
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Expert Comment

by:Peter Kwan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The tool is perfect. Thanks.

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now