将regexp从JavaScript转换为PCRE格式


Convert regexp from JavaScript to PCRE format

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

我得到了这个{{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> 

我认为[^]的用法是误导/不清楚,我不确定它的意思。

(因为你得到一个关于缺失的]的错误,我假设它打开一个字符类,^是类的否定,下面的]不关闭类,它是它的第一个成员。但是因为Regexr使用它的方式不同,我认为在不同的regex引擎中含义不同。)

你可以试试这个

{{Infobox[^{]*?({{[^}]*?}}['s'S]*?)*}}

查看Regexr

我想说问题是^, {}字符没有被转义-你应该试试:

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

编辑以匹配[^]:

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