doudui6756 2013-09-07 12:08
浏览 20
已采纳

Wikitext到HTML的正则表达式[关闭]

I am Tryting to show wiki text in HTML in my app(Using cakephp). I use this expression for <h4>,<h5>,<h6>

'/^==== (.*) ====$/'
    =>  '<h4>\1</h4>',
    '/^===== (.*) =====$/'
    =>  '<h5>\1</h5>',
    '/^====== (.*) ======$/'
    => '<h6>\1</h6>',

[http://www.wikipedia.org Wikipedia] will be shown as <ahref="www.wikipedia.org">Wikipedia </a>

[5|Text] will show like <a href="http://fillpage/5/page_slug">Text</a>

[[6#HeadingB|Text ]] will shown as <ahref="http://fillpage/6/page_slug#HeadingB>Text</a>

[[6#HeadingB]] will shown as <a href="http://fillpage/5/page_slug">PageTitle</a>

[media:631|Description|Position] will shown as <img class="position" src="http://fullpath/lang_code/631.jpg">Description

I wan't to know how to make such regex for links(exactly like Wikipedia shows.) how to make it done. And how to match space b/w two brackets for links.

  • 写回答

1条回答 默认 最新

  • duanju8308 2013-09-07 18:54
    关注

    Match \[(.*?) +(.*)\]

    and replace with : $2

    or if you're using preg in php

    Go for this:

    '/\[(.*?) +(.*)]$/'

    and accept them with backslash like so : '<a href="\1">\2</a>' within an array as the value to the pattern above as a key.

    That was up for wikipedia. You can go to http://gskinner.com/RegExr/ and use the pattern references on the right column.

    Hope it helps.

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

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应