有效的RegEx在CSS属性值后强制使用单个空格 [英] Efficient RegEx to Force Single Space After CSS Attribute Value

查看:91
本文介绍了有效的RegEx在CSS属性值后强制使用单个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我经常在Dreamweaver中从事基本CSS工作一样……DW代码完成往往会在冒号后不加空格的情况下直接添加CSS属性值……{attr:value}。由于使用挑剔的浏览器会出现渲染问题,这引起了一些真正的调试难题,因此我经常发现自己不得不在冒号后手动添加空格。 [^ DRY]

As I often work in Dreamweaver for basic CSS work ... DW code completion tends to add CSS attribute values directly after the colon without spacing ... {attr:value}. As this has caused some real debugging headaches due to rendering issues with picky browsers, I often find myself having to manually add the space after the colon. [^D.R.Y.]

我目前正在构建一个简单的正则表达式代码段,可以运行该代码段以正确格式化CSS ...

I'm currently working on building a simple regex snippet that I can run to format the CSS properly ...

我目前似乎可以使用的东西,但是我认为这有点粗糙...我基本上只是想看看是否有任何建议...这就是我所得到的...

What I currently have seems to work but I think it's a bit crude ... I'm basically just trying to see if there are any suggestions ... Here's what I've got ...

(.*?(?={).*?:)([^\s][\w!#$%&'*+/=?^_`{|}~-]*.*?;}) 

并用...代替行

 \s$2 

►澄清►下面我对上面的解决方案进行了某种改进……尽管如此,我仍然认为它需要改进……

► Clarification ► Below I've Somewhat Improved the Solution Above ... I Still Think It Needs Refinement Though ...

(?:(?={*))(?:\s*?)([\s\r\n]*?\b[\w!#$%&'*+/=?^_`|~-]*:(?!.*{))(?!\s)(.*?;) 

并替换为...

$1 $2

►Perl版本►

Perl对此做得很简单...

Perl makes very light work of this ...

perl -pi -e 's/^(.*?:)([^\s].*?;).*$/$1 $2/ig'


推荐答案

如果您在C#程序中运行正则表达式代码段,那么我将为您提供解决方案:

If you run your regex snippet in a C# program, I have the solution for you:

Regex regex = new Regex(@"(?<=\{{1}[\s\r\n]*)((?<Attribute>[^}:\n\r\s]+):\s+(?<Value>[^;]*);[\s\r\n]*)*(?=\})");

regex.Replace(input, match =>
{
    var replacement = string.Empty;
    for (var i = 0; i < match.Groups["Attribute"].Captures.Count; i++)
    {
        replacement += string.Format(CultureInfo.InvariantCulture, "{0}: {1}\r\n", match.Groups["Attibute"].Captures[i], match.Groups["Value"].Captures[i]);
    }
    return replacement;
});

这篇关于有效的RegEx在CSS属性值后强制使用单个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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