【问题标题】:Need a simple Regular Expressions here这里需要一个简单的正则表达式
【发布时间】:2009-06-17 07:25:00
【问题描述】:

我终于解析了维基百科的 wiki 文本。我在这里有以下类型的文本:

{{Airport-list|the Solomon Islands}}

* '''AGAF''' (AFT) – [[Afutara Airport]] – [[Afutara]]
* '''AGAR''' (RNA) – [[Ulawa Airport]] – [[Arona]], [[Ulawa Island]]
* '''AGAT''' (ATD) – [[Uru Harbour]] – [[Atoifi]], [[Malaita]]
* '''AGBA''' – [[Barakoma Airport]] – [[Barakoma]]

我需要检索单个数组中以模式开头的所有行

* '''

我认为这里会调用一个正则表达式来订购,但我的正则表达式部分确实搞砸了。

另外,在另一个示例中,我有以下文本:

{{otheruses}}
{{Infobox Settlement
|official_name          = Doha
|native_name        = {{rtl-lang|ar|الدوحة}} ''ad-Dawḥa''
|image_skyline          = Doha Sheraton.jpg
|imagesize              = 
|image_caption          = West Bay at night
|image_map              = QA-01.svg
|mapsize                = 100px
|map_caption            = Location of the municipality of Doha within [[Qatar]].
|pushpin_map            =
|pushpin_label_position = 
|pushpin_mapsize        = 
|subdivision_type       = [[Countries of the world|Country]]
|subdivision_name       = [[Qatar]]
|subdivision_type1      = [[Municipalities of Qatar|Municipality]]
|subdivision_name1      = [[Ad Dawhah]]
|established_title      = Established
|established_date       = 1850
|area_total_km2         = 132
|area_total_sq_mi       = 51
|area_land_km2          = 
|area_land_sq_mi        = 
|area_water_km2         = 
|area_water_sq_mi       = 
|area_water_percent     = 
|area_urban_km2         = 
|area_urban_sq_mi       =
|area_metro_km2         = 
|area_metro_sq_mi       = 
|population_as_of       = 2004
|population_note        = 
|population_footnotes = <ref name=poptotal>[http://www.planning.gov.qa/Qatar-Census-2004/Flash/introduction.html Qatar 2004 Census]</ref>
|population_total       = 339847
|population_metro       = 998651
|population_density_km2 = 2574
|population_density_sq_mi = 6690
|latd=25 |latm=17 | lats=12 |latNS=N 
|longd=51|longm=32 | longs=0| longEW=E 
|coordinates_display    = inline,title
|coordinates_type       = type:city_region:QA
|timezone               = [[Arab Standard Time|AST]]
|utc_offset             = +3
|website                = 
|footnotes              = 
}} <!-- Infobox ends -->
'''Doha''' ({{lang-ar|الدوحة}}, ''{{transl|ar|ad-Dawḥa}}'' or ''{{unicode|ad-Dōḥa}}'') is the [[capital city]] of [[Qatar]].  It has a population of 400,051 according to the 2005 census,<ref name="autogenerated1">[http://www.hotelrentalgroup.com/Qatar/Sheraton%20Doha%20Hotel%20&%20Resort.htm Sheraton Doha Hotel & Resort | Hotel discount bookings in Qatar<!-- Bot generated title -->]</ref> and is located in the [[Ad Dawhah]] municipality on the [[Persian Gulf]].  Doha is Qatar's largest city, with over 80% of the nation's population residing in Doha or its surrounding [[suburbs]], and is also the economic center of the country. 
It is also the seat of government of Qatar, which is ruled by [[Sheikh Hamad bin Khalifa Al Thani]]–the current ruling Emir of Qatar. 

我需要在这里提取信息框。信息框包含并包含第一次出现

之间的所有文本
{{Infobox Settlement

并以第一次出现的

结束
}} <!-- Infobox ends -->

当谈到正则表达式时,我完全迷失了,我可以在这里使用帮助。我正在使用 PHP。


编辑!帮助!

我已经战斗了 40 个小时,但我无法让愚蠢的正则表达式正常工作:( 到目前为止,我只有这个:

{{信息框[^\b(\r|\n)}}(\r|\n)\b]*[\b(\r|\n)}}(\r|\n)( \r|\n)\b]

但它不起作用我希望它读取 {{infobox 和以 \n}} 结尾之间的所有字符串数据\n

我正在使用 Php,但无法让它工作 :( 它只是返回第一次出现的 }},而忽略了我希望它检索 }} 的事实。在我浪费更多之前请帮助我理智点:'(

【问题讨论】:

  • 这里有两个问题?

标签: php regex string wiki


【解决方案1】:

我需要提取信息框...

试试这个,这次确保 dotall 模式已启用

\{\{Infobox.*?(?=\}\} <!-- Infobox ends -->)


再次解释一下:

(?xs)    # x=comment mode, s=dotall mode
\{\{     # two opening braces (special char, so needs escaping here.)
Infobox  # literal text
.*?      # any char (including newlines), non-greedily match zero or more times.
(?=      # begin positive lookahead
\}\}     # two closing braces
<!-- Infobox ends --> # literal text
)        # end positive lookahead

这将匹配(但不包括)结尾表达式 - 如果需要,您可以删除前瞻本身并仅包含内容以使其包含结尾。

更新,根据评论回答:

\{\{Infobox.*?(?=\n\}\}\n)

与上面相同,但先行查找各自行上的两个大括号。

也可以选择允许评论,使用:

\{\{Infobox.*?(?=\n\}\}(?: <!-- Infobox ends-->)?\n)

【讨论】:

  • 谢谢,但信息框的问题在于并非所有页面的信息框都以 注释结尾。我注意到的信息框肯定以两个大括号 }} 结尾,前后各有一个换行符,即 \n}}\n 诀窍是字符串中可以有大括号,但其中的大括号在同一行。 - 我该如何解决这个..
  • 正如你所建议的 - 在之前和之后使用 \n - 所以转义变成 \n\}\}\n
  • 在 .Net 4 中我发现我需要使用 "[\s\S]" 而不是 "."出于某种原因。
【解决方案2】:

MediaWiki 是开源的。看看他们的source code ... ;-)

【讨论】:

  • 没有比实际实现更好的地方了。 :)
【解决方案3】:

我认为最好的方法是将所有行合并为一个字符串,尤其是对于信息框。

然后是类似的东西

$reg = "\n(\* '''[^\n]*)";

对于第一部分(新行之后的所有内容,以 * ''' 开头并且不是新行)。

对于第二部分,我现在还不确定,但这是一个玩玩的好地方: http://www.solmetra.com/scripts/regex/index.php

下面是正则表达式语法的简短参考: http://www.regular-expressions.info/reference.html

【讨论】:

    【解决方案4】:

    我需要检索单个数组中以模式* ''' 开头的所有行

    启用多行模式并确保 dotall 模式禁用,然后使用:

    ^\* '''.*$
    


    剖析的表达式是:

    (?xm-s) # Flags:
            # x enables comment mode (spaces ignore, hashes start comments)
            # m enables multiline mode (^$ match lines)
            # -s disables dotall (. matches newline)
    ^       # start of line
    \*      # literal asterisk
    [ ]     # literal space (needs braces in comment mode, but not otherwise)
    '''     # three literal apostrophes
    .*      # any character (excluding newline), greedily matched zero or many times.
    $       # end of line
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多