找到你要的答案

Q:regex, I want to return (n) values in a submatch

Q:正则表达式,我想回到一个匹配项的值(N)

I have the following regex, that I want to extract from a text some template tags with (n) parameter:

\{{2}([\w\-\d]+)([\s:\,]*(?:\[([\w\%]+)\])*)*\}{2}

But the problem is the above regex returns only the last parameter, and I want to catch all parameters. This parameters also can repeat multiple times. I can't see what is wrong. Somebody can help me?

Exemples:

Exemple 1 {{url-builder: [linktitle], [email]}} // Return ['url-builder', 'email']
Exemple 2 {{url-builder: [linktitle]}} // Return ['url-builder', 'linktitle']

Exemple 1 should return ['url-builder', 'link-title', 'email']

To be more specific, I have the follow text

Ao contrário do que se acredita, Lorem Ipsum não é simplesmente um texto randômico. Com mais de 2000 anos, {{url-builder: [linktitle], [email]}} suas raízes podem ser encontradas em uma {{author-name}} obra de literatura latina clássica datada de 45 AC. Richard McClintock, um professor de latim do Hampden-Sydney College na Virginia,

From the above text, I want to catch all tags {{tag}}. But some tags can have parameters {{tag: parameter-1}}, and these parameters can repeat multiple times {{tag: parameter-1: parameter-1}}. I'm trying to catch a single array with the tag and all parameters.

我有下面的正则表达式,我想从一些文字模板标签(N)参数提取:

\{{2}([\w\-\d]+)([\s:\,]*(?:\[([\w\%]+)\])*)*\}{2}

但问题是上面的正则表达式只返回最后一个参数,我想抓住所有的参数。这个参数也可以重复多次。我看不出什么是错的。有人能帮我吗?

Exemples:

Exemple 1 {{url-builder: [linktitle], [email]}} // Return ['url-builder', 'email']
Exemple 2 {{url-builder: [linktitle]}} // Return ['url-builder', 'linktitle']

例1返回[ 'url-builder ','链接标题”,“电子邮件”]

更具体地说,我有以下的文字

AO控制á里约做阙本身acredita,乱数假文NãOésimplesmente嗯该兰德ôMICO。COM©2000年,{ { URL Builder:[ linktitle ]、[电子邮件] } } SUAS RAíZES PODEM SER encontradas EM UMA { {作者姓名} }走去拉丁文字CLáSSICA datada德45 AC Richard McClintock,嗯,汉普登悉尼大学做教授的latim Na弗吉尼亚,

从上面的文本中,我想捕获所有标签{标记} }。但一些标签可以有参数{ {标签:parameter-1 } },而这些参数可以重复多次{ {标签:parameter-1:parameter-1 } }。我试图用标签和所有参数捕捉一个数组。

answer1: 回答1:

Yes, capturing groups usually store only the last occurrence of their pattern. You can wrap all parameters in one group to get them:

\{\{([\w\-]+):\s*((?:\[[\w\%]+\][,\s]*)*)\}\}
    ^       ^    ^                      ^

Then afterwards split that by /[,\s]+/ to get a list.

是的,捕获组通常只存储模式的最后一次发生。您可以在一组中封装所有参数以获得它们:

\{\{([\w\-]+):\s*((?:\[[\w\%]+\][,\s]*)*)\}\}
    ^       ^    ^                      ^

然后将其拆分为[,],得到一个列表。

php  regex