Reputation: 2044
If anyone can help me having a lot of trouble with a regex expression
Basically I need a RegEx that can spot files in html,css,javascript enclosed by single or double quotes
I have got this far (\"|')([^"|'|\s]|\\"*)*\..*(\"|')
I am using C#
See the link https://regex101.com/r/nga5yF/2
But if you look at my tests at the bottom where I have multiple matches on a single line it fails.
Any help would be appreciated!
Upvotes: 1
Views: 157
Reputation: 18980
We can use a negated character class for this:
['"][^'" ]+?\.[^'" ]*?['"]
Explanation:
.
Upvotes: 2
Reputation: 28509
Instead of *
use the non-greedy or lazy *?
quantifier to match an unlimited number of repetitions, but in a non-greedy way. (i.e. take the shortest match).
Also, you forgot to exclude whitespace and quotes in the part after requiring a dot to be included.
Test this version of the regex:
(?<quote>\"|\')(?<file>[^\"\'\s]*?\.[^\"\'\s]*?)\k<quote>
https://regex101.com/r/wTXhaM/1
Further improvements:
Or if you want to also match filenames where single and double quotes are mixed use this variant:
(?:\"|\')(?<file>[^\"\'\s]*?\.[^\"\'\s]*?)(?:\"|\')
https://regex101.com/r/uM2Qfd/1
Upvotes: 1