将regexp从JavaScript转换为PCRE格式

时间:2011-07-11 06:00:03

标签: php regex pcre

在这里看到很多关于同一主题的问题,但没有找到答案:(

我有{{Infobox[^]*?({{[^{}]*?}}[^]*?)*}}正则表达式。在Javascript和http://www.gskinner.com/RegExr/

中作为魅力

在PHP中,它会在偏移量为41的字符类中产生错误“编译失败:缺少终止”。

我知道分隔符,也试过在{和}之前放置\ - 没有帮助。

{{For|the title track of the album|Master of Puppets (song)}}
{{Infobox Album <!-- See Wikipedia:WikiProject_Albums -->
| Name        = Master of Puppets
| Type        = Studio album
| Artist      = [[Metallica]]
| Cover       = Metallica - Master of Puppets.jpg
| Released    = {{Start date|1986|3|3}}
| Recorded    = September 1 – December 27, 1985 at [[Sweet Silence Studios]], [[Copenhagen]], [[Denmark]]
| Genre       = [[Thrash metal]]
| Length      = 54:41
| Label       = [[Elektra Records|Elektra]], [[Music for Nations]], [[Vertigo Records|Vertigo]]
| Producer    = Metallica, [[Flemming Rasmussen]]
| Last album  = ''[[Ride the Lightning]]''<br/>(1984)
| This album  = '''''Master of Puppets'''''<br/>(1986)
| Next album  = ''[[...And Justice for All (album)|...And Justice for All]]''<br/>(1988)
| Misc        = {{Singles
  | Name           = Master of Puppets
  | Type           = Studio
  | single 1       = [[Master of Puppets (song)|Master of Puppets]]
  | single 1 date  = July 2, 1986
  | single 2       = [[Battery (song)|Battery]]
  | single 2 date  = 1986
  | single 3       = [[Welcome Home (Sanitarium)]]
  | single 3 date  = 1986
  }}
}}
'''''Master of Puppets''''' is the third studio album by the American [[heavy metal music|heavy metal]] band [[Metallica]]. It was released on March 3, 1986 through [[Elektra Records]]. The album reached #29<ref>{{Cite news 
  | last = Pareles
  | first = Jon
  | coauthors = 
  | title = HEAVY METAL, WEIGHTY WORDS
  | work = [[The New York Times]]
  | place = USA
  | page = 8
  | language = 
  | publisher = The New York Times Company
  | date = 10 July 1988
  | url = http://www.nytimes.com/1988/07/10/magazine/heavy-metal-weighty-words.html?pagewanted=8
  | accessdate = 14 November 2010}}</ref> on the U.S. [[Billboard 200|''Billboard'' 200]] album chart and was the band's first gold record for sales of over 500,000 copies. This was done without any radio airplay or the release of a music video. The album eventually was certified 6x platinum by the [[Recording Industry Association of America|RIAA]].<ref>{{cite web|title=Gold & Platinum|url=http://riaa.com/goldandplatinumdata.php?resultpage=1&table=SEARCH_RESULTS&action=&title=Master%20of%20Puppets&artist=Metallica&format=&debutLP=&category=&sex=&releaseDate=&requestNo=&type=&level=&label=&company=&certificationDate=&awardDescription=&catalogNo=&aSex=&rec_id=&charField=&gold=&platinum=&multiPlat=&level2=&certDate=&album=&id=&after=&before=&startMonth=1&endMonth=1&startYear=1958&endYear=2008&sort=Artist&perPage=25|publisher=RIAA|accessdate=2009-12-31}}</ref> 

2 个答案:

答案 0 :(得分:1)

我说问题是^{}个字符未被转义 - 您应该尝试使用:

\{\{Infobox[\^]*?(\{\{[\^\{\}]*?\}\}\[^]*?)*\}\}

修改以匹配[^]的任何字符:

\{\{Infobox.*?(\{\{[.\{\}]*?\}\}\.*?)*\}\}

答案 1 :(得分:1)

我认为[^]的用法具有误导性/不清楚,我不确定其含义。

(因为您收到有关缺少]的错误,我认为它会打开一个字符类,^是该类的否定以及以下{{1}没有关闭类,它是它的第一个成员。但是因为Regexr使用它的方式不同,我认为不同的正则表达式引擎的含义不同。)

你可以试试这个

]

here on Regexr

相关问题