dptj13337 2012-07-18 16:03
浏览 135
已采纳

模式匹配css规则

I have the following pattern:

[\{\}].*[\{\}]

With the following test strings (can provide more if needed):

}.prop{hello:ars;} //shouldn't match
}#prop{} //should match
}.prop #prop {} //should match

The purpose of the pattern is to find empty css rulesets. Can someone suggest how I go about excluding matches with characters between the second set of brackets? I will be updating the pattern as I get closer to a solution.

edit: on http://gskinner.com/RegExr/ this pattern: [\}].*[\{]{1}[/}]{1} seems to have the desired result although it is breaking when transfered to php for reasons I don't understand.

edit: first apologies if this should be a separate question. Using the pattern in the first edit in php:

    $pattern = "/[\}].*[\{]{1}[/}]{1}/";
    preg_match_all ($pattern, $new_css, $p);
    print_r($p);

When $new_css is a string of the content of an uploaded css file containing empty rulesets, $p is never populated. Yet I know this pattern is ok. Can anyone see what the issue is?

edit: final solution

//take out other unwanted characters
        $pattern = "/\}([\.#\w]\w+\s*)+{}/";
        //do it twice to beat any deformation
        $new_css = preg_replace ($pattern, '}', $new_css);
        $new_css = preg_replace ($pattern, '}', $new_css);
  • 写回答

2条回答 默认 最新

  • drne47241 2012-07-18 17:26
    关注

    Try the pattern: '/}([\.#]\w+\s*)+{}/'

    $new_css = "{}.prop{hello:ars;}
    {}#prop{} //should match
    }.prop #prop {} //should match
    }.prop { aslkdfj}
    }.prop { }
    ";
    
    $pattern = '/}([\.#]\w+\s*)+{}/';
    preg_match_all ($pattern, $new_css, $p);
    print_r($p);
    

    This outputs:

    Array
      (
        [0] => Array
        (
          [0] => }#prop{}
          [1] => }.prop #prop {}
        )
    
        [1] => Array
        (
          [0] => #prop
          [1] => #prop
        )
      )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建