从css文件中提取和转换url [英] Extracting and transforming url from a css file

查看:165
本文介绍了从css文件中提取和转换url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在css文件中,我有一些带有背景图片网址的样式。此网址可以有三种形式:

images / logo.png - >该图片位于css位置'子文件夹'图像"
/styles/images/logo.png - >图片是从根网络应用程序搜索
http://myapp/styles/images/logo.png - >图片是使用其完整的绝对网址搜索。

有时,有一个引用,有时双引号有时没有任何围绕网址...

我想转换css文件为了远程访问图片,请使用最后一个表格(绝对网址)的所有网址。

我正在尝试使用以下代码片段来实现此结果:

1 乌里cssFolder;
2 < font style ="color:blue"> if (Uri.TryCreate(cssFileUri, "。 " out cssFolder))
3 {
4 sb.AppendLine(Regex.Replace(
5 downloadedCssContent,
6 @ " url \((?< char> ['\" <字体样式="font-size:11px">"])?(?< url>。*)\ k< char > \)" ;,
7 ? @ " url(" + cssFolder.ToString()。TrimEnd('\\')+ @ " $ {url})"
8 ));
9
10 }
11 else
< nobr> 12 {
13
14 }

解决方案

我将你的正则表达式模式略微修改为:@" url\((小于?炭> ['"" ])(小于?URL> *的 的)\k<炭> ;? \)&


所涉及的变化:
1。删除\"因为你使用的是@ verbatim字符串,你可以使用2个双引号"""而不是逃避它。
2。解决了没有删除结束字符的问题我添加了一个"?"在"url"的末尾命名组使其不那么贪婪,只匹配尽可能少的重复。通过这样做它不会捕获最终的反向引用的char匹配。

我不确定你的cssFolder路径是什么样的。我认为它类似于我在下面的"absolutePath"中使用的内容。变量。也许以下内容适合您的需求,适用于您网址的所有3种变体:

< font style ="font-size:11px"> string absolutePath = " http:// myapp / styles / images /" ;
string < font style ="font-size:11px"> [] sampleUrls = {
" url(images / logo.png)" " url('images / logo.png')" ; " url(/styles/images/logo.png)"
@ < font style ="color:blue">" url(" " /styles/images/logo.png" ")" " url(http:// myapp / styles / images / logo.png)"
};
Regex rx = new 正则表达式(@ " url \((?< char> ['" "])?(?< url>。*?)\ k< char > \)");?
foreach string url sampleUrls)
{
string currentUrl = rx.Match(url).Groups [ " url" ]。值;
//使用匿名委托代替MatchEvaluator来使用currentUrl变量 < font style ="font-size:11px">
string result = rx.Replace(url,
委托
{
//前置当前网址的绝对路径
//仅使用最后一个/
int lastIndex = currentUrl.LastIndexOf('/');
return < font style ="font-size:11px"> String.Format( " {0} {1}" ,absolutePath,
currentUrl.Substring(lastIndex的+ 1,currentUrl.Length - lastIndex的 - 1)
);
}
);
Console.WriteLine(" URL = {0}" ,currentUrl);
Console.WriteLine(" Result = {0}" ,结果);
Console.WriteLine();
}
Console.Read() ;

Hi,

In a css file, I have some styles with background image url. This url can have three forms :

images/logo.png --> the picture is located in the css location' child folder "images"
/styles/images/logo.png --> the picture is search from the root web application
http://myapp/styles/images/logo.png --> the picture is search using its full absolute url.

and sometimes, there is a quote, sometimes a double quote sometimes nothing around the url ...

I want to transform the css file in order to have all urls in the last form (absolute url) in order to access remotely to the picture.

I'm trying to achieve this result using the following code snippet :

1                    Uri cssFolder; 
2                    if (Uri.TryCreate(cssFileUri, "."out cssFolder)) 
3                    { 
4                        sb.AppendLine(Regex.Replace( 
5                            downloadedCssContent, 
6                            @"url\((?<char>['\""])?(?<url>.*)\k<char>?\)", 
7                            @"url(" + cssFolder.ToString().TrimEnd('\\') + @"${url})" 
8                            )); 
9 
10                    } 
11                    else 
12                    { 
13 
14                    } 

解决方案

I modified your regex pattern slightly to: @"url\((?<char>['""])?(?<url>.*?)\k<char>?\)"

The changes involved:
1. Removing the \" part since you're using an @ verbatim string you could just use 2 double quotes "" instead of escaping it.
2. Fixing the issue where the closing char wasn't being removed I added a "?" at the end of the "url" named group to make it less greedy and only match as few repetitions as possible. By doing so it won't capture the final backreferenced char match.

I'm not sure what your cssFolder path looks like. I assume it's similar to what I have used below in the "absolutePath" variable. Perhaps the following will suit your needs and will work for all 3 variations of your URL:

string absolutePath = "http://myapp/styles/images/"
string[] sampleUrls = { 
    "url(images/logo.png)""url('images/logo.png')""url(/styles/images/logo.png)"
    @"url(""/styles/images/logo.png"")""url(http://myapp/styles/images/logo.png)" 
}; 
Regex rx = new Regex(@"url\((?<char>['""])?(?<url>.*?)\k<char>?\)"); 
 
foreach (string url in sampleUrls) 
    string currentUrl = rx.Match(url).Groups["url"].Value; 
    // use an anonymous delegate in place of a MatchEvaluator to make use of currentUrl variable 
    string result = rx.Replace(url, 
        delegate 
        { 
            // prepend the absolute path to the current url 
            // only use the portion of the current url that occurs after the last / 
            int lastIndex = currentUrl.LastIndexOf('/'); 
            return String.Format("{0}{1}", absolutePath, 
                currentUrl.Substring(lastIndex + 1, currentUrl.Length - lastIndex - 1) 
            ); 
        } 
    ); 
 
    Console.WriteLine("URL = {0}", currentUrl); 
    Console.WriteLine("Result = {0}", result); 
    Console.WriteLine(); 
Console.Read(); 


这篇关于从css文件中提取和转换url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆