我正在閱讀一個 HTML 檔案,試圖從中獲取一些資訊。我嘗試過 HTML 決議器,但不知道如何使用它們來獲取關鍵文本。原始版本讀取 html 檔案,但此版本是 StackOverflow 目的的最小作業示例。
#!/usr/bin/env perl
use 5.036;
use warnings FATAL => 'all';
use autodie ':default';
use Devel::Confess 'color';
sub regex_test ( $string, $regex ) {
if ($string =~ m/$regex/s) {
say "$string matches $regex";
} else {
say "$string doesn't match $regex";
}
}
# the HTML text is $s
my $s = ' rs577952184 was merged into
<a target="_blank"
href="rs59222162">rs59222162</a>
';
regex_test ( $s, 'rs\d was merged into.*\<a target="_blank". href="rs(\d )/');
但是,這不匹配。
我認為問題是“合并”后的換行符不匹配。
如何更改上述正則運算式以匹配$s?
uj5u.com熱心網友回復:
問題是 中的尾隨/字符$regex,應該省略或更改為"
uj5u.com熱心網友回復:
use strict;
use warnings;
use feature 'say';
my $s = ' rs577952184 was merged into
<a target="_blank"
href="rs59222162">rs59222162</a>
';
my $re = qr/rs\d was merged into\s <a target="_blank"\s href="rs(\d )">rs\d <\/a>/;
regex_test($s,$re);
exit 0;
sub regex_test {
my $string = shift;
my $regex = shift;
say $string =~ m/$regex/s
? "$string matches $regex"
: "$string doesn't match $regex";
}
輸出
rs577952184 was merged into
<a target="_blank"
href="rs59222162">rs59222162</a>
matches (?^:rs\d was merged into\s <a target="_blank"\s href="rs(\d )">rs\d </a>)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/528212.html
標籤:正则表达式perl
