[英]Regex expression to match string between two other strings
Another hard regex in javascript: JavaScript中的另一个硬正则表达式:
I have this string: 我有这个字符串:
<td style="padding:0 2%">{% for product in products.pos01 %}
<table class="box-item" item-editable="" style="float:left;padding-bottom:10px; width:32%;border-spacing: 0px; border: 0px; border-collapse: collapse;"><tr><td style=" padding:10px 5px; vertical-align:top ">
<a href="**|product.url|**" class="button view-deal" style="padding:8px 10px; background-color: #fc5d26; text-decoration:none; display:inline-block; text-align:center; color:#fff; font-size:12px;margin:0px; line-height:140%;text-transform:uppercase">view deal</a></div></td></tr></table>{% if (loop.index0-2) is divisibleby 3 %}</td></tr><tr>
<td style="padding:0 2%">{% endif %}{% endfor %}
</td>
I'm trying to get content inside any loops from the string {% for ... %} and {% endfor %} 我正在尝试从字符串{%for ...%}和{%endfor%}的任何循环中获取内容
I've tried with, but can't get 我已经尝试过,但是无法
/({% for (!?%})+ %})((!?endfor)*){% endfor %}/gm
/({%for(!?%})+%})((!? endfor)*){%endfor%} / gm
but didn't work 但是没用
I think you suppose to use a pattern similar to this 我认为您应该使用与此类似的模式
Regex 正则表达式
(?<={% for[^%]*%})((?:.|\n)*)(?={% endfor %})
Explanation 说明
(?<={% for[^%]*%})
: use lookbehind to searching for pattern {% for[^%]*%}
(?<={% for[^%]*%})
:使用后向搜索模式{% for[^%]*%}
(?={% endfor %})
: use lookahead to searching for text {% endfor %}
(?={% endfor %})
:使用超前搜索文本{% endfor %}
((?:.|\\n)*)
: a message between a for loop which is captured by variable $1
((?:.|\\n)*)
:for循环之间的一条消息,由变量$1
捕获
But in case if your language do not support lookaround, you just use this 但是如果您的语言不支持环视,则只需使用
Regex 正则表达式
({% for[^%]*%})((?:.|\n)*)({% endfor %})
Explanation 说明
({% for[^%]*%})
: capture pattern {% for[^%]*%}
to variable $1
({% for[^%]*%})
:捕获模式{% for[^%]*%}
到变量$1
({% endfor %})
: capture pattern {% endfor %}
to variable $3
({% endfor %})
:捕获模式{% endfor %}
到变量$3
((?:.|\\n)*)
: a message between a for loop which is captured by $2
((?:.|\\n)*)
:for循环之间的一条消息,由$2
捕获
Just modify my regex
according to a restriction in your language then you will done this. 只需根据您所用语言的限制修改我的
regex
,即可完成此操作。
Edit 编辑
As I search, I think Javascript
do not support lookaround and another thing {
, }
need to be escape by \\
. 在搜索时,我认为
Javascript
不支持环视,并且{
}
需通过\\
进行转义。 I already tested regex using some online regex tester for Javascript and I get this. 我已经使用一些在线正则表达式测试仪对Java进行了正则表达式测试,我明白了。
(\{% for[^%]*%\})((?:.|\n)*)(\{% endfor %\})
to get a text that you want, you just use variable $2
. 要获得所需的文本,只需使用变量
$2
。
Additional 额外
In case you want to capture message inside nested loop eg 如果您想捕获嵌套循环内的消息,例如
Example Message 消息示例
{% for product in products.pos01 %}
...
{% for product in products.pos02 %}
"messages"
{% endfor %}
...
{% endfor %}
To capture "messages"
inside this nested loop you just need to modify my previous regex to 要在此嵌套循环中捕获
"messages"
您只需将我以前的正则表达式修改为
(\{% for[^%]*%\}(?:.|\n)*\{% for[^%]*%\})((?:.|\n)*)(\{% endfor %\}(?:.|\n)*\{% endfor %\})
Explanation 说明
(\\{% for[^%]*%\\}(?:.|\\n)*\\{% for[^%]*%\\})
: means "start of for loop" following by "any characters including newline" and following by "start of for loop" (\\{% for[^%]*%\\}(?:.|\\n)*\\{% for[^%]*%\\})
:表示“ for循环的开始”,后跟“包括换行符在内的任何字符”和“开始for循环”
((?:.|\\n)*)
: our target message ((?:.|\\n)*)
:我们的目标消息
(\\{% endfor %\\}(?:.|\\n)*\\{% endfor %\\})
: means "end of for loop" following by "any characters including newline" and following by "end of for loop" (\\{% endfor %\\}(?:.|\\n)*\\{% endfor %\\})
:表示“ for循环结束”,后跟“包括换行符的任何字符”,后跟“ for循环结束”
Notice that I just rearrange my previous regex to done this little bit more complicated job. 注意,我只是重新排列了我以前的正则表达式来完成这项稍微复杂的工作。
Try this regex: 试试这个正则表达式:
{% for [^\\0]+?{% endfor %}
Explanation: 说明:
{% for # search for text `{% for `
[^\0]+? # while not input's end
{% endfor %} # search for the next `{% endfor %}`
Or, if you want groups: 或者,如果您要分组:
{% (for [^\0]+?)%}([^\0]+?){% endfor %}
Hope it helps. 希望能帮助到你。
If you're trying to get what is within {% for ... %}
and {% endfor %}
this might work: 如果您尝试获取
{% for ... %}
和{% endfor %}
则可能会起作用:
/%}([\W\w]*){%/gm
Explanation : 说明 :
%} matches the characters %} literally
\W match any non-word character [^a-zA-Z0-9_]
\w match any word character [a-zA-Z0-9_]
{% matches the characters {% literally
g modifier: global. All matches (don't return on first match)
m modifier: multi-line.
Example : 范例 :
https://regex101.com/r/pM3oX7/1 https://regex101.com/r/pM3oX7/1
It's unclear if you are wanting to get the text within the %} {%
or also include that part. 尚不清楚您是否要在
%} {%
内获取文本,还是要包含该部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.