Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regular Expression needed.

Posted on 2011-03-02
8
Medium Priority
?
938 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 500 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 500 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
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!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
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

972 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