1
1. <link href="~/styles/test.css?v=15" rel="stylesheet" />
2. <link href="../styles/test.css?v=15" rel="stylesheet" />
3. <link href="~/styles/test.css" rel="stylesheet" />
4. <link href="../styles/test.css" rel="stylesheet" />
5. <link href="https://static.content.com/styles/test.css" rel="stylesheet" />
6. <link href="@Url.Content("~/styles/test.css")" rel="stylesheet" type="text/css" />
7. <link href="@Url.Content("../styles/test.css")" rel="stylesheet" type="text/css" />
8. <link href="@Url.Content("https://static.content.com/styles/test.css")" rel="stylesheet" type="text/css" />
9. <link type="text/css" rel="stylesheet" 
href='<%= Page.ResolveClientUrl("~/styles/test.css") %>' />
10. <link type="text/css" rel="stylesheet" 
href='<%= Page.ResolveClientUrl("../styles/test.css") %>' />

In all the above case, I should get value inside inverted comas like

~/styles/test.css?v=15
../styles/test.css?v=15
~/styles/test.css
../styles/test.css
https://static.content.com/styles/test.css

Can I get a single Regex for this? I just want value inside inverted comas and not anything other than that, not even inverted comas and css can be referred from other folders like content and not only styles. so please do not provide Regex, which looks like hard coding. There can be many syntax for referring the css, I should not change Regex for every syntax.

2
  • FYI Added explanation about how the regex works. Commented Apr 21, 2014 at 11:27
  • Krishna I have edited the regex to cover the last case you added. Does this answer your question? Commented Apr 22, 2014 at 19:36

2 Answers 2

1

EDIT: Krishna has added a new context in which he wants to match css, so the regex has been expanded accordingly.

In most standard regex flavors, I would use this:

(?<=ResolveClientURL\(")[^"]+|(?:(?<=<link href=")(?!@Url\.Content)|(?<=<link href="@Url\.Content\("))(?:[^"])+

The left side of the alternation handles the last case you added by using a lookbehind.

On the other side of the alternation, the first set of parentheses contains an alternation | with two sets of lookarounds, one for each of our two cases.

The first one asserts: "at this position in the string, the string <link href=" is behind me, and the string @Url\.Content is not in front of me.

After the OR (|), the second one asserts: "at this position in the string, the string <link href="@Url\.Content\(" is behind me.

Having asserted either of those, we know we just passed the opening double quote.

The (?:[^"])+ eats up any character that is not a double quote, thereby eating up everything up to the closing "

Sign up to request clarification or add additional context in comments.

5 Comments

This wont work, if I am using @Url.Content because it will give me @Url.Content( output.
@Krishna You are absolutely right. It was late last night and I didn't notice the @Url cases. I fixed it and explained how it works.
Pls check my updated question. Your regex will change for every syntax then.
@Krishna Not fair to change the specs of the question like that... Are you saying that my solution didn't work? Or that you changed your mind about what you want to match? I've tried to help, but it's getting unclear what you want to match.
No, Not at all, But how can I provide so many syntaxes which are used to refer css. I had already mentioned to provide Regex, which can get css references in whichever format it is and not for particular syntax. Your answer is correct, but It cant be used to get css references in many scenarios. I know this many above syntax, but one can use many more, so need regex to get css references, in any way a user may write, because I cannot change Regex for every syntax, right?
0

use this :

"((?:https?|\.{2}|~).*?\/styles.*?)"

demo here : http://regex101.com/r/iJ4oT7

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.