doumi0737 2010-02-10 23:48
浏览 62
已采纳

用于在PHP中解析注释变量的正则表达式

I have a comment block that can look like this;

/**
 * variable1: value
 * variable2: value
 */

or like this;

/*
variable1: value
variable2: value
*/

What I need is to be able to match any number of variable/value pairs and add them to an array. I can't seem to figure it out though, I keep matching the wrong things.

All variables would be single-line, so that should simplify things a little. Spaces before 'variable' or after the the colon should be disregarded, but any other spaces in the value lines should be retained.

UPDATE:

What I ended up going with was a slight expansion of the selected answer;

/(\w)*\s*:\s*([\w'"\/.: ]*)/

It allowed for URLs to be used as values like so;

/**
 * url: 'some/file.png'
 * url: "http://www.google.ca/intl/en_ca/images/logo.gif"
 */
  • 写回答

2条回答 默认 最新

  • doujiao1948 2010-02-10 23:54
    关注

    Does this not work? (Assuming multi-line matching enabled)

    (\w)*\s*:\s*(\w*)
    

    I assume you pulled off the comment block with something like

    \/\*.*?\*\/
    

    with . set to match anything.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算