質問

ついに、ウィキペディアのウィキテキストを解析しています。ここには次の種類のテキストがあります:

{{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時間戦いましたが、愚かな正規表現を正しく動作させることはできません:(これまでのところ、これだけです:

{{Infobox [^ \ b(\ r | \ n)}}(\ r | \ n)\ b] * [\ b(\ r | \ n)}}(\ r | \ n)( \ r | \ n)\ b]

しかし、動作しない{{infoboxで終わり、\ n}}で終わるすべての文字列データを読み取りたい\ n

私はPhpを使用していますが、これを機能させることができません:(それは、}}を前の改行で取得したいという事実を無視して、}}の最初の出現を返すだけです。これに対する正気: '(

役に立ちましたか?

解決

  

情報ボックスを抽出する必要があります...

これを試してみてください。今回は、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)

上記と同じですが、lookaheadは独自の行で2つのブレースを探します。

オプションでコメントも許可するには、次を使用します:

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

他のヒント

MediaWiki はオープンソースです。 ソースコード ...;-)

最善の方法は、特にインフォボックスの場合、すべての行を1つの文字列にマージすることだと思います。

次に、線に沿って何か

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

最初の部分(* '' 'で始まり改行ではない新しい行の後のすべて)。

2番目の部分については、今は確信が持てませんが、ここは少し遊んでみるのに最適な場所です。 http://www.solmetra.com/scripts/regex/index.php

そして、正規表現の構文の簡単なリファレンスを次に示します。 http://www.regular-expressions.info/reference.html

  

パターン * '' '

で始まる単一の配列内のすべての行を取得する必要があります

マルチラインモードを有効にし、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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top