duanmi3476 2011-01-22 10:38
浏览 38
已采纳

Reg-Ex用于过滤,解析和替换特定字符串? (PHP)

While developing a private CMS for a client, I've had an idea to implement a php-underlying, yet server-side and flexible "language".

I'm in trouble finding a reqular-expression finding (filter..) the following string ( [..] is the code, which'll be parsed after it's been filtered out ), I want to filter the string out with the line-breaks.

<(
    [..]
)>

I was looking for a solution all night, but I didn't find a solution.

  • 写回答

1条回答 默认 最新

  • douwang4374 2011-01-22 10:50
    关注

    First off: Listen to Dan Grossmans advice above.

    From my current understanding of your question, you want to get the verbatim content between <( and )> - no exceptions, no comment handling.

    If so, try this RegExp

    '/<\(((?:.|\s)*?)\)>/'
    

    which you can use like this

    preg_match_all('/<\(((?:.|\s)*?)\)>/', $yourstring, $matches)
    

    It doesn't need case insensitivity, and it does lazy matching (so you can apply it to a string with several instances of matches).


    Explanation of the RegExp: Starting with <(, ending with )> (brackets escaped of course), in between is the capturing group. At its core, we take either regular characters . or whitespace \s (which solves your problem, since line breaks are whitespace too). We don't want to capture every single character, so the inner group is non capturing - just either whitespace or character: (?:.|\s). This is repeated any number of times (including zero), but only until the first match is complete: *? for lazy 0-n. That's about it, hope it helps.

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

报告相同问题?

悬赏问题

  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R