duankeng1911 2015-05-14 12:47
浏览 63
已采纳

RegEx匹配CSS文件中的特定javaDoc属性,并提取有关css定义的数据

I'm trying to store some meta data about CSS styles, which our app can use to build menus that allow the user to select those styles (amongst other things).

I thought the JavaDoc style comment was the best approach, and use an attribute like config or similar. Then store a JSON style definition as the value of that property.

I've been trying to write some RegEx (PHP) to do the following:

  1. Find all JavaDoc comments with @config attribute.
  2. Extract the value of @config as the JSON object
  3. Then match the following css TAG and CLASS names if they are defined under it.

So this comment and class definition could be extracted into 3 matches.

/**
 * @config {name:'Orange Title', order:1}
 */
h1.title_orange {

}
  • Match 1 : {name:'Orange Title', order:1}
  • Match 2 : h1
  • Match 3 : title_orange

What makes it more complex is the JSON part could be multiline, and the multilines may or may not contain the *.

  • 写回答

1条回答 默认 最新

  • dtpxi88884 2015-05-14 13:31
    关注

    The biggest task is on the assumptions you make (which things to test):

     (?:^\/\*\*[\s
    ]+\*\s@config\s(.*)$|^\s+\*\/[\s
    ]+[\#\.]?([\w-]+)\s*(?:[\#\.]*([\w-]+)\s*)*{$)
    

    You can test it in this Rubular.

    I assumed that you have config in the first comment line of the javaDoc, I also assumed that you may have multiple spaces randomly between these, that your words/classes/ids may have -.

    What more do you need?

    EDIT

    This works great in Rubular. Although regex should be the same across languages, this doesn't seem to work in PHP Live RegexAlexander 4 mins ago

    You're right, so I compacted it into one match group and this one seems to be working (you have to click preg_match_all tab):

    \/[*]{2}[\s
    ]+\*\s@config\s(.*)[\s
    ]+.*[\s
    ]+\*\/[\s
    ]+[\#\.]?([\w-]+)\s*(?:[\#\.]*([\w-]+)\s*)*{
    

    Which means, I am considering the javaDoc and the CSS together (javaDoc with several lines). Still, there may be adjustments that need to be made.

    EDIT2

    Thats amazing, thanks so much! What if the @config wasn't always the first entry in the comment? Is that still possible ? – Matt Bryson 7 mins ago

    It is:

    \/[*]{2}(?:[\s
    ]+.*)*[\s
    ]+\*\s@config\s(.*)[\s
    ]+.*[\s
    ]+\*\/[\s
    ]+[\#\.]?([\w-]+)\s*(?:[\#\.]*([\w-]+)\s*)*{
    

    You can try this one PHP Live Regex in preg_match_all tab.

    EDIT3

    Matt evolved his own regex to something simpler. The problem seems that the capture groups cannot be repeated indefinitely with this (to get all CSS classes/ids):

    (?:[\#\.\,]([\w-]+)\s*)*
    

    https://regex101.com/r/jM0yH0/6

    Therefore, this still needs to be solved...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑