热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

这里需要一个简单的正则表达式-NeedasimpleRegularExpressionshere

Imfinallyparsingthroughwikipediaswikitext.Ihavethefollowingtypeoftexthere:我终于通过wikip

I'm finally parsing through wikipedias wiki text. I have the following type of text here:

我终于通过wikipedias 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]]

I need to retrieve all lines in a single array which start with the pattern

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

* '''

I think a regular expression would be called to order here but I'm really messed up on my regular expressions part though.

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

Plus in another example I have the following text:

另外在另一个例子中,我有以下文字:

{{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 = [http://www.planning.gov.qa/Qatar-Census-2004/Flash/introduction.html Qatar 2004 Census]
|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              = 
}} 
'''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,[http://www.hotelrentalgroup.com/Qatar/Sheraton%20Doha%20Hotel%20&%20Resort.htm Sheraton Doha Hotel & Resort | Hotel discount bookings in Qatar] 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. 

I need to extract the infobox here. The infobox is and includes all text between the first occurrence of

我需要在这里提取信息框。信息框是并且包括第一次出现之间的所有文本

{{Infobox Settlement

and ends with the first occurrence of

并以第一次出现结束

}} 

I'm totally lost when it comes to regular expressions and I could use help here. I'm using Php.

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


EDIT! HELP!

I've been battling for 40 hours and I can't get the stupid regular expression to work right :( so far I just have this:

我一直在争斗40个小时,我不能让愚蠢的正则表达式正常工作:(到目前为止,我只是这样:

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

But its not working I want it to read all the string data between {{infobox and ends with a \n}}\n

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

I'm using Php and can't get this to work :( It just returns the first occurrence of }} ignoring the fact that I want it to retrieve }} with preceding linefeed. Help please before I waste more of my sanity on this :'(

我正在使用Php并且不能让它工作:(它只是返回第一次出现}}忽略了我希望它用前面的换行检索}}的事实。请帮助之前我更浪费我的理智:'(

4 个解决方案

#1


I need to extract the infobox ...

我需要提取信息框......

Try this, this time making sure dotall mode is enabled:

试试这个,这一次确保启用了dotall模式:

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


And again, explanation for that:

再次,解释:

(?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
 # literal text
)        # end positive lookahead

This will match upto (but excluding) the the ending expression - you could remove the lookahead itself and include just the contents to have it include the ending, if necessary.

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

Update, based on comment to answer:

更新,根据评论回答:

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

Same as above, but lookahead looks for two braces on their own line.

与上面相同,但是lookahead在他们自己的行上寻找两个大括号。

To optionally allow the comment also, use:

要同时允许评论,请使用:

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

#2


MediaWiki is open-source. Have a look at their source code ... ;-)

MediaWiki是开源的。看看他们的源代码...... ;-)

#3


I think the best way is to merge all lines into one string, especially for the infobox.

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

Then something along the lines of

然后是一些东西

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

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

for the first part (everything after a new line that start with * ''' and is not a new line).

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

And for the second part I'm not quire sure right now, but this is a nice place to play around a bit: http://www.solmetra.com/scripts/regex/index.php

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

And here is a short reference for regular expression syntax: http://www.regular-expressions.info/reference.html

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

#4


I need to retrieve all lines in a single array which start with the pattern * '''

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

Enable multiline mode and ensure dotall mode is disabled, and use this:

启用多线模式并确保禁用dotall模式,并使用:

^\* '''.*$


That expression dissected is:

解剖的表达是:

(?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

推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了Oracle存储过程的基本语法和写法示例,同时还介绍了已命名的系统异常的产生原因。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • [echarts] 同指标对比柱状图相关的知识介绍及应用示例
    本文由编程笔记小编为大家整理,主要介绍了echarts同指标对比柱状图相关的知识,包括对比课程通过率最高的8个课程和最低的8个课程以及全校的平均通过率。文章提供了一个应用示例,展示了如何使用echarts制作同指标对比柱状图,并对代码进行了详细解释和说明。该示例可以帮助读者更好地理解和应用echarts。 ... [详细]
  • 正则表达式及其范例
    为什么80%的码农都做不了架构师?一、前言部分控制台输入的字符串,编译成java字符串之后才送进内存,比如控制台打\, ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • Parity game(poj1733)题解及思路分析
    本文是对题目"Parity game(poj1733)"的解题思路进行分析。题目要求判断每次给出的区间内1的个数是否和之前的询问相冲突,如果冲突则结束。本文首先介绍了离线算法的思路,然后详细解释了带权并查集的基本操作。同时,本文还对异或运算进行了学习,并给出了具体的操作步骤。最后,本文给出了完整的代码实现,并进行了测试。 ... [详细]
author-avatar
无情的有情人家_834
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有