1

I'm trying to find a CSS URL using a regex but having no experience with it, and I'm lost.

After research , I've made this:

^(href)([\S]*(\.css))(\")$

but it works only with

href="/media/system/css/modal.css"

and I've got to find it in:

<link rel="stylesheet" href="/media/system/css/modal.css" type="text/css" />

or

<link rel="stylesheet" href="http://www.campingbellavista.ch/compon [...] k2/css/k2.css" type="text/css" />
Kobi
  • 135,331
  • 41
  • 252
  • 292
Yvalf
  • 13
  • 2
  • 3
    If you specify ^ and $ you are stating that the pattern has to starts form the beginning of the line and ends at the end of the line. Try to remove ^ and $ from you regex. And consider to check at the reply of this question:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags. Being french should not affect how Regex works ;) – Felice Pollano Jun 21 '11 at 08:46

6 Answers6

1

Maybe simplify? ]*href="([^"]+)"

An awesome place to screw around with your regexes I've found was http://www.rubular.com/ .

Xedecimal
  • 3,153
  • 1
  • 19
  • 22
1

Consider using a parser like the HTML Agility Pack and some XPath:

HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://stackoverflow.com/questions/6422517");
var links = doc.DocumentNode.SelectNodes("//link[@rel='stylesheet']");
foreach (var node in links)
{
    Console.WriteLine(node.Attributes["href"].Value);
}

Or, to select all elements with href that ends with .css:

var links = doc.DocumentNode.DescendantNodesAndSelf()
               .Where(node => node.Attributes.Contains("href") && 
                              node.Attributes["href"].Value.EndsWith(".css"));
Kobi
  • 135,331
  • 41
  • 252
  • 292
  • Couldn't get that second xpath to work on HAP, to be honest. Should be something like `//link[ends-with(@href,'css')]`. – Kobi Jun 21 '11 at 09:17
1

Your Pattern

^(href)([\S]*(\.css))(\")$

^ means match the start of the string ==> NOT true in your second example

$ means match the end of the string ==> NOT true in your second example

You can just delete thos anchors from your pattern and it should match the second example.

You have a lot of unnecessary brackets in your expression. This is doing the same

href\S*\.css\"

The () brackets have the meaning that they group the content and put the matched pattern into a capturing group. I assume you don't want this, at least not the way you used them.

probably you want something like this

href=\"(\S*\.css)\"

so the hyperlink is in the capturing group 1.

The [] creates a character class, but thats not needed if you put only one item into it. So [\S] is the same than \S.

But at last I would suggest, that you have a closer look at Kobi's answer.

stema
  • 90,351
  • 20
  • 107
  • 135
0

how about this: href="([^"]*)"

duedl0r
  • 9,289
  • 3
  • 30
  • 45
0

Try this:

href="([^"]*?\.css)"
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
0

Try:

Regex expression = new Regex("<.*href=\"(?<CSSUrl>.*css)\".*", RegexOptions.None);

With that you can get the URL with the groupname "CSSUrl".