【问题标题】:Write a regular expression to capture a substring from html [duplicate]编写正则表达式以从 html 中捕获子字符串 [重复]
【发布时间】:2015-02-28 06:56:41
【问题描述】:

我必须使用正则表达式从 html 文本中找到特定模式

例如:

我的字符串是

<table border="0" cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td><p align="justify"><u>Counsel appeared</u></p><p align="justify"><a name="COUNSEL" id="COUNSEL"></a>K. P. Garg CA<b>for the Appellant</b>.: A. K. Monga, Sr. DR <b>for theRespondent</b></p><p align="justify"><b><u><a name="JUDGE" id="JUDGE"></a>R. P.TOLANI, JM.</u></b></p><p align="justify">testing</p>..........and so on 

我想从 html 文本中删除 &lt;p align="justify"&gt;&lt;u&gt;Counsel appeared&lt;/u&gt;&lt;/p&gt;&lt;p align="justify"&gt;&lt;a name="COUNSEL" id="COUNSEL"&gt;&lt;/a&gt;K. P. Garg CA&lt;b&gt;for the Appellant&lt;/b&gt;.: A. K. Monga, Sr. DR &lt;b&gt;for theRespondent&lt;/b&gt;&lt;/p&gt; 这部分。 html标签中包含的文本是动态的。

为此我写了以下正则表达式

gsub(/<p align="justify"><u>counsel appeared<\/u><\/p><p align="justify"><a name="counsel" id="counsel"><\/a>.*<b>.*<\/b><\/p>/i, '')

但它从“律师”中删除了整个文本出现

直到结束。

那么我如何从上面的 html 字符串中删除特定部分? 谁能帮我修改我的正则表达式?

【问题讨论】:

标签: regex ruby-on-rails-4


【解决方案1】:

尝试以下模式:

&lt;p\s+?align\s*?=\s*?"justify"&gt;\s*?&lt;u&gt;\s*?counsel appeared\s*?&lt;\/u&gt;\s*?&lt;\/p&gt;\s*?&lt;p\s+?align\s*?=\s*?"justify"&gt;\s*?&lt;a\s+?name\s*?=\s*?"counsel"\s+?id\s*?=\s*?"counsel"&gt;\s*?&lt;\/a&gt;.*?&lt;b&gt;.*?&lt;\/b&gt;\s*?&lt;\/p&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 2014-09-15
    • 1970-01-01
    相关资源
    最近更新 更多