text-extraction 相关问题

文本提取是从非结构化和/或半结构化机器可读文档(文本)中自动提取结构化信息的任务。

从 HTML 超链接获取可见文本

如何匹配链接元素中的文本“Cats”? 猫论坛

回答 4 投票 0

从 HTML 字符串中的所有超链接获取 href 值[重复]

我有多个包含链接的字符串,例如: 测试2 布拉布拉 我有多个包含链接的字符串,例如: <A HREF="http://www.testings2">testings2</A> <A HREF="http://www.blabla">blabla</A> <A HREF="http://www.gowick">gowick</A> 我想使用正则表达式模式来获取 href 属性声明中的 URI。 我可以这样做: /".*?"/ 但是“”就会出现。有没有一种方法可以在不使用 preg_replace() 函数的情况下获取 HREF="" 中的 URI? preg_match_all('/href="([^"]+)/i', $str, $matches); var_dump($matches); 不确定如何在 PhP 中应用它,但它可以在 perl 中使用 /<a href="([^"]+)".+/i; 我认为是这样 preg_match( '/<a href="([^"]+)".+/i;', $str, $matches); $str=<<<EOF <A href="http:// www.testings2">testings2</A> blah <A HrEF= "http://www.blabla">blabla</A> blah <A HREF="http://www.gowick">gowick</A> <A HREF="http://www.testing3">testing3</A> <a class="navigation" id="selected" href="http://somewhere.com"><xsl:value-of select="title" /></a> EOF; $s = preg_split("/<\/A>/i",$str); $s = preg_replace("/\n+/","",$s); $uris = preg_grep("/HREF/i",$s); foreach($uris as $v){ $fin = explode('">',$v); $t=preg_split('/href="/i',$fin[0] ); print end($t)."\n"; } 输出 # php test.php http://www.testings2 http://www.blabla http://www.gowick http://www.testing3 http://somewhere.com

回答 3 投票 0

从文件路径字符串获取文件名并删除不需要的尾随字符

我想捕获没有年份的路径中的最后一个文件夹。对于这个字符串路径,我只需要 Millers Crossing 而不是 Movies\Millers Crossing,这是我当前的正则表达式捕获的内容。 G:\莫...

回答 4 投票 0

解析HTML文档并获取包含指定内容的节点值

我有 3 个消息块。 例子: 只是文字。 我有 3 个消息块。 示例: <!-- message --> <div> Just the text. </div> <!-- / message --> <!-- message --> <div> <div style="margin-left: 20px; margin-top:5px; "> <div class="smallfont">Quote:</div> </div> <div style="margin-right: 20px; margin-left: 20px; padding: 10px;"> Message from <strong>Nickname</strong> &nbsp; <div style="font-style:italic">Hello. It's a quote</div> <else /></if> </div> <br /><br /> It's the simple text </div> <!-- / message --> <!-- message --> <div> Text<br /> <div style="margin:20px; margin-top:5px; background-color: #30333D"> <div class="smallfont" style="margin-bottom:2px">PHP code:</div> <div class="alt2" style="margin:0px; padding:6px; border:1px inset; width:640px; height:482px; overflow:auto; background-color:#FFFACA;"> <code style="white-space:nowrap"> <div dir="ltr" style="text-align:left"> <!-- php buffer start --> <code> LALALA PHP CODE </code> <!-- php buffer end --> </div> </code> </div> </div><br /> <br /> More text </div> <!-- / message --> 我正在尝试为这些块创建正则表达式,但不起作用。 preg_match('#<!-- message -->(?P<text>.*?)</div>.*?<!-- / message -->#is', $str, $s); 它仅适用于第一个块.. 如何让正则表达式检查消息或php代码中是否有引号? (?P<text>.*?) for text (?P<phpcode>.*?) for php code (?P<quotenickname>.*?) for quoted nickname (?P<quotemessage>.*?) for quote message 等等... 非常感谢!!!! onteria_的变化 <!-- message --> <div> Just the text. <b>bold text</b><br/> <a href="link">link</a>, <s><i>test</i></s> </div> <!-- / message --> 输出: Just the text , 我需要什么来修正这个结论,以及“a”、“b”、“s”、“i”等等。 如何确保 html 没有被删除? 注意到那些关于不使用正则表达式的回复了吗?这是为什么?那是因为 HTML 代表结构。老实说,HTML 代码过度使用 div 而不是使用语义标记,但无论如何我都会使用 DOM 函数来解析它。那么,这是我使用的示例 HTML: <html> <body> <!-- message --> <div> Just the text. </div> <!-- / message --> <!-- message --> <div> <div style="margin-left: 20px; margin-top:5px; "> <div class="smallfont">Quote:</div> </div> <div style="margin-right: 20px; margin-left: 20px; padding: 10px;"> Message from <strong>Nickname</strong> &nbsp; <div style="font-style:italic">Hello. It's a quote</div> </div> <br /><br /> It's the simple text </div> <!-- / message --> <!-- message --> <div> Text<br /> <div style="margin:20px; margin-top:5px; background-color: #30333D"> <div class="smallfont" style="margin-bottom:2px">PHP code:</div> <div class="alt2" style="margin:0px; padding:6px; border:1px inset; width:640px; height:482px; overflow:auto; background-color:#FFFACA;"> <code style="white-space:nowrap"> <div dir="ltr" style="text-align:left"> <!-- php buffer start --> <code> LALALA PHP CODE </code> <!-- php buffer end --> </div> </code> </div> </div><br /> <br /> More text </div> <!-- / message --> </body> </html> 现在查看完整代码: $doc = new DOMDocument(); $doc->loadHTMLFile('test.html'); // These just make the code nicer // We could just inline them if we wanted to // ----------- Helper Functions ------------ function HasQuote($part, $xpath) { // check the div and see if it contains "Quote:" inside return $xpath->query("div[contains(.,'Quote:')]", $part)->length; } function HasPHPCode($part, $xpath) { // check the div and see if it contains "PHP code:" inside return $xpath->query("div[contains(.,'PHP code:')]", $part)->length; } // ----------- End Helper Functions ------------ // ----------- Parse Functions ------------ function ParseQuote($quote, $xpath) { // The quote content is actually the next // next div over. Man this markup is weird. $quote = $quote->nextSibling->nextSibling; $quote_info = array('type' => 'quote'); $nickname = $xpath->query("strong", $quote); if($nickname->length) { $quote_info['nickname'] = $nickname->item(0)->nodeValue; } $quote_text = $xpath->query("div", $quote); if($quote_text->length) { $quote_info['quote_text'] = trim($quote_text->item(0)->nodeValue); } return $quote_info; } function ParseCode($code, $xpath) { $code_info = array('type' => 'code'); // This matches the path to get down to inner most code element $code_text = $xpath->query("//div/code/div/code", $code); if($code_text->length) { $code_info['code_text'] = trim($code_text->item(0)->nodeValue); } return $code_info; } // ----------- End Parser Functions ------------ function GetMessages($message, $xpath) { $message_contents = array(); foreach($message->childNodes as $child) { // So inside of a message if we hit a div // We either have a Quote or PHP code, check which if(strtolower($child->nodeName) == 'div') { if(HasQuote($child, $xpath)) { $quote = ParseQuote($child, $xpath); if($quote['quote_text']) { $message_contents[] = $quote; } } else if(HasPHPCode($child, $xpath)) { $phpcode = ParseCode($child, $xpath); if($phpcode['code_text']) { $message_contents[] = $phpcode; } } } // Otherwise check if we've found some pretty text else if ($child->nodeType == XML_TEXT_NODE) { // This might be just whitespace, so check that it's not empty $text = trim($child->nodeValue); if($text) { $message_contents[] = array('type' => 'text', 'text' => trim($child->nodeValue)); } } } return $message_contents; } $xpath = new DOMXpath($doc); // We need to get the toplevel divs, which // are the messages $toplevel_divs = $xpath->query("//body/div"); $messages = array(); foreach($toplevel_divs as $toplevel_div) { $messages[] = GetMessages($toplevel_div, $xpath); } 现在让我们看看 $messages 是什么样子的: Array ( [0] => Array ( [0] => Array ( [type] => text [text] => Just the text. ) ) [1] => Array ( [0] => Array ( [type] => quote [nickname] => Nickname [quote_text] => Hello. It's a quote ) [1] => Array ( [type] => text [text] => It's the simple text ) ) [2] => Array ( [0] => Array ( [type] => text [text] => Text ) [1] => Array ( [type] => code [code_text] => LALALA PHP CODE ) [2] => Array ( [type] => text [text] => More text ) ) ) 以消息分隔,再进一步分解为消息中的不同内容!现在我们甚至可以使用像这样的基本打印功能: foreach($messages as $message) { echo "\n\n>>>>>> Message >>>>>>>\n"; foreach($message as $content) { if($content['type'] == 'text') { echo "{$content['text']} "; } else if($content['type'] == 'quote') { echo "\n\n======== Quote =========\n"; echo "From: {$content['nickname']}\n\n"; echo "{$content['quote_text']}\n"; echo "=====================\n\n"; } else if($content['type'] == 'code') { echo "\n\n======== Code =========\n"; echo "{$content['code_text']}\n"; echo "=====================\n\n"; } } } echo "\n"; 我们明白了! >>>>>> Message >>>>>>> Just the text. >>>>>> Message >>>>>>> ======== Quote ========= From: Nickname Hello. It's a quote ===================== It's the simple text >>>>>> Message >>>>>>> Text ======== Code ========= LALALA PHP CODE ===================== More text 这一切再次有效,因为 DOM 解析函数能够理解结构。

回答 1 投票 0

获取自定义开始 HTML 标签及其结束标签之间的文本

$data = "你好"; preg_match_all("/\[.]+\<\/Data\>/", $data, $match); print_r($match); 这将返回: 数组 ( [0] => 数组 ( ) ) 所以我猜...

回答 6 投票 0

获取指定的自定义 HTML 标签的属性和内部 HTML

假设我执行 preg_replace() 如下: preg_replace ("/(.*)<\/my_tag>/U", "$1", $sourse); 这有效,但我也这样做...

回答 3 投票 0

从包含命名空间静态方法调用的字符串中获取子字符串

我被正则表达式困住了。 $匹配=数组(); // $controller = $this->getRequest()->attributes->get('_controller'); $controller = "Acme\MyBundle\Controller\MyController::

回答 1 投票 0

从描述配方成分的字符串中获取带有计量单位的数值表达式

我需要一个正则表达式,给定成分行,它会告诉我该成分的数量。以下是一些示例值: 8盎司半甜巧克力 6 个鸡蛋 3/4 杯糖 1 汤匙

回答 1 投票 0

获取较大字符串中两个字符串之间的字符[重复]

我希望能够在 PHP 中使用正则表达式来提取 以下 html 片段中的“Ruby9” 关于 Red Hot Ruby Jewelry代码:Ruby9 我希望能够在 PHP 中使用正则表达式来提取 以下 html 片段中的“Ruby9” on Red Hot Ruby Jewelry<br>Code: Ruby9<br> 有时“代码”将是字母数字、数字或只是字母。 尝试这个正则表达式: $str = "on Red Hot Ruby Jewelry<br>Code: Ruby9<br>"; $pattern = "/Code: ([^<]+)/"; // matches anything up to first '<' if(preg_match($pattern, $str, $matches)) { $code = $matches[1]; // matches[0] is full string, matches[1] is parenthesized match } else { // failed to match code } if (preg_match('/(?<=: ).+(?=<)/', $subject, $regs)) { // ow right! match! $result = $regs[0]; } else { // my bad... no match... } 如果模式始终相同,则正则表达式将如下所示: "<br>Code: ([a-zA-Z0-9]+)<br>" 这将捕获 Code: 之后和 之前的任何字母或字母数字或数字字符串。尝试以下操作: <?php $subject = "on Red Hot Ruby Jewelry<br>Code: Ruby9<br>"; $pattern = '<br>Code: ([a-zA-Z0-9]+)<br>'; preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE); print_r($matches); ?>

回答 3 投票 0

如何从此代码中获取重要文本

另一个正则表达式问题。这是我的 HTML 输入 - 一些文字 这是非常重要的文字 这是我需要的另一篇重要文本。 请帮助...

回答 2 投票 0

从字符串中提取“M d, Y”格式的日期表达式

我的网页是这样的 标题 不重要 2012 年 11 月 20 日 我只想获取日期并将其格式化为新的日期时间。

回答 1 投票 0

获取括号内的字符串[重复]

我有一个像这样的字符串: 我的文本 (1,151) 我想使用正则表达式仅获取 ( 和 ) 之间的值,在本例中仅:1,151。

回答 2 投票 0

从 HTML 字符串中包裹在 <strong> 标签中的所有 <p> 标签中获取文本

我在 PHP 方面有点挣扎。 我创建了一个数组并用一些curl 返回数据填充了一些位置。 我不知道如何在每个数组位置中搜索 并返回... 我在 PHP 方面有点挣扎。 我创建了一个数组,并用一些curl返回数据填充了一些位置。 我不知道如何在每个数组位置中搜索 <p><strong> 并将其中的每个字符返回到 </p>。 从终端我可能会做这样的事情: grep -A 2 strong | sed -e 's/<p><strong>//' -e 's/<\/strong><br\/>//' -e 's/<br \/>//' -e 's/<\/p>//' -e 's/--//' -e 's/^[ \t]*//;s/[ \t]*$//' 但是我在 PHP 中迷失了 有什么建议吗? 编辑:我想要每个<p><strong>的内容到</p> 编辑2:这是我正在尝试的代码: $m=array(); preg_match_all('/<p><strong>(.*?)<\/p>/',$buffer,$m); $sizeM = count($m); for ( $counter2 = 0; $counter2 <= $sizeM; $counter2++) { $displayString.= $m[$counter2]; } 并获取 ArrayArrayArray...作为我的 $displayString 编辑3:我正在这样做: $curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL, $url); curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.04 (lucid) Firefox/3.6.15"); curl_setopt($curl_handle, CURLOPT_HEADER, 0); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($curl_handle); curl_close($curl_handle); $m=array(); preg_match_all('/<p>.*?<strong>(.*?)<\/p>/i',$buffer,$m); foreach($m[1] as $mnum=>$match) { $displayString.='Match '.$mnum.' is: '.$match."\n"; } 在 PHP 和许多其他语言中,最好不要使用字符串函数或正则表达式来匹配 HTML,因为 HTML 不是正则的,并且可能会出现真正的错误。 您应该看到的是一个 DOM 系统,您可以将 html 作为对象进行迭代,就像 JavaScript 访问 DOM 一样。 您应该查看以下本机 PHP 库来帮助您入门:http://php.net/manual/en/class.domdocument.php 您可以像这样简单地使用: $xml = new DOMDocument(); // Load the url's contents into the DOM $xml->loadHTMLFile($url); //Loop through each <a> tag in the dom and add it to the link array foreach($xml->getElementsByTagName('a') as $link) { echo $link->href . "\n"; } 这将找到文档中的所有链接。 另请参阅我创建的帖子以及 Gordon 的精彩回答:How do you parse and process HTML/XML in PHP? preg_match_all() $m=array(); preg_match_all('/<p>\s*<strong>([\s\S]*?)<\/p>/i',$string,$m); foreach($m[1] as $mnum=>$match){ $displayString.='Match '.$mnum.' is: '.$match."\n"; } $m 现在包含所有匹配项。 $m[0] 持有整场比赛, $m[1] 保存括号匹配项 正如其他帖子中所指出的,如果您尝试处理 HTML,则不应使用正则表达式。 要处理查找<p><strong>,您可以使用DOMDocument: $doc = new DOMDocument(); $doc->loadHTML($html); $pTags = $doc->getElemetsByTagName('p'); for ($pTags as $pTag) { if ($pTag->firstChild->nodeName === 'strong') { $data = $pTag->firstChild->nodeValue; } } 或者使用 XPath: $doc = new DOMDocument(); $doc->loadHTML($html); $xpath = new DOMXPath($doc); $matchingNodes = $xpath->query('//p/strong'); 或者您甚至可以使用外籍人士。 这些方法比使用正则表达式更清晰、经过验证、灵活且更安全。 我个人最喜欢从 xml 样式文档中提取数据的是 xpath。 这是一组很好的 xpath 示例:http://msdn.microsoft.com/en-us/library/ms256086.aspx 编辑: *注意:如果您尝试处理非常大的 XML/HTML 文档,您将不想使用 DOMDocument 或 XPath,因为它们对于大型文档可能会很慢。 对于这些情况,请使用事件驱动的 XML 解析器。 我们在工作中遇到过使用 XPath 解析大型 XML 文件需要几分钟的情况,而使用事件驱动解析器解析同一文件只需要几秒钟。 正则表达式将是你的朋友。 strpos、substr 和 explode 是有用的 php 函数。 好吧,如果位置与您期望的结果不相关,您可以尝试将数组合并到单个字符串中,并在其中执行正则表达式... 这是代码 <?php $data = array( 'DONT MATCH THISDONT MATCH THIS<p><strong>hello1!</strong></p>DONT MATCH THISDONT MATCH THISDONT MATCH THIS', 'DONT MATCH THISDONT MATCH THIS<p><strong>hello2!</strong></p>DONT MATCH THISDONT MATCH THISDONT MATCH THIS', 'DONT MATCH THISDONT MATCH THIS<p><strong>hello3!</strong></p>DONT MATCH THISDONT MATCH THISDONT MATCH THIS', '<p><strong>hello4!</strong></p>DONT MATCH THISDONT MATCH THIS<p><strong>hello5!</strong> test test</p>DONT MATCH THISDONT MATCH THISDONT MATCH THIS', 'DONT MATCH THISDONT MATCH THIS<p><strong>hello6!</strong></p>DONT MATCH THISDONT MATCH THISDONT MATCH THIS', ); preg_match_all('/<p><strong>.*?<\/p>/',implode($data,''),$results); print_r($results); ?> 让我知道这是否适合您。干杯!

回答 5 投票 0

R 函数提取列表中的两个单词短语,其中还包含第一个单词作为单独的字符串

我有一个带有字符串列的数据框,以及我想从该列中提取的单词/短语列表。我使用了以下代码。 df <- data.frame(string = c("A rose is a...

回答 2 投票 0

如何使用golang从pdf中提取文本?

我正在尝试从 golang 中的 pdf 文件中提取文本。请参阅下面的代码。由于某种原因,它打印出完整的垃圾(一些随机数)。这是pdf。我相信可以提取...

回答 3 投票 0

从 PDF 文档中提取日期时间列

我正在尝试使用“str_extract_all”函数从R中的PDF文档中提取数据。我正在尝试查找日期时间字段,该字段以以下格式显示在文档中: 呃……

回答 1 投票 0

Pandas 提取电话号码(如果格式正确)

我有一列包含电话号码。它们的格式通常为 (555) 123-4567,但有时它们的格式不同或者不是正确的数字。我正在尝试将此字段转换为...

回答 4 投票 0

Pandas 提取电话号码(如果格式正确)

我有一列包含电话号码。它们的格式通常为 (555) 123-4567,但有时它们的格式不同或者不是正确的数字。我正在尝试将此字段转换为...

回答 1 投票 0

如何从 AWS Textract 分析的文档中按阅读顺序打印表格和行

我正在使用 AWS Textract 从 pdf 文档中提取文本和表格。 我需要可以解析提取的文本、提取的表格并按顺序打印一个字符串中的所有内容的代码...

回答 1 投票 0

使用 Poppler 从 PDF 中提取文本(C++)

我正在尝试了解 Poppler 及其(缺乏)文档。 我想做的是一件非常简单的事情:打开一个PDF文件并阅读其中的文本。然后我将处理文本,但是...

回答 3 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.