How to get regular expression to stop at first occurrence and then start searching from there, leading to multiple results

huangapple go评论98阅读模式
英文:

How to get regular expression to stop at first occurrence and then start searching from there, leading to multiple results

问题

正则表达式:

<h1.*?<\/h1>

要搜索的内容:

<h1 style=""></h1><span> alsjd alkj dalsd a</span><h1>asdsadsa</h1>

结果应该是两个起始和结束h1标签的匹配项:

<h1 style=""></h1> 和 
<h1>asdsadsa</h1>
英文:

I have a regular expression to search for complete h1 tags (from start of <h1> to end of </h1>) in a content but it searches for the whole content

Regular expression

<h1.*<\/h1>

Content to search for

<h1 style=""></h1><span> alsjd alkj dalsd a</span><h1>asdsadsa</h1>

The result is one match only from first occurrence of starting h1 to last occurrence of ending h1. It matches the whole content.

I want it to give me 2 occurrence of starting and ending h1 tags
like the result should be

<h1 style=""></h1> and 
<h1>asdsadsa</h1>

答案1

得分: 1

我认为你需要将 .* 变为非贪婪模式

使用这个

<h1.*?<\/h1>
英文:

I think you've to make .* non greedy

use this

<h1.*?<\/h1>

huangapple
  • 本文由 发表于 2023年6月22日 13:17:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定