Regex 从文件中的文本匹配电话号码
我需要有关Perl代码的帮助 我想搜索一个文件并逐行读取,如果电话号码的父项匹配,则必须将该行打印到另一个文件中 我能够匹配字符串,但不确定:如何匹配电话号码的格式/模式 电话号码可以不同。我只想遵循电话号码匹配的格式 数字的示例可以是xx xxx xxxx 这是我的代码看一看Regex 从文件中的文本匹配电话号码,regex,perl,Regex,Perl,我需要有关Perl代码的帮助 我想搜索一个文件并逐行读取,如果电话号码的父项匹配,则必须将该行打印到另一个文件中 我能够匹配字符串,但不确定:如何匹配电话号码的格式/模式 电话号码可以不同。我只想遵循电话号码匹配的格式 数字的示例可以是xx xxx xxxx 这是我的代码看一看 #!//usr/bin/perl # Saving The Aurgument Values To Local Variables Sent from the Command line $source
#!//usr/bin/perl
# Saving The Aurgument Values To Local Variables Sent from the Command line
$source = $ARGV[0];
$pattren = $ARGV[1];
$destination = $ARGV[2];
$matches = 0;
# Using open function to open the file and the pointer will be save to variable $fh
# die condition used with a message is the file name give from the command line could
# doesn't exsist
open($sr, '<', $source) or die "Could not open file $source ";
open($ds, '>', $destination);
# Using While loop condition as long as we are getting data per line.
while ($line = <$sr>){ #While reading one line at a time from the log....
if ($line =~ m/$pattren/i) {
#Printing the Mached line Content Of the source File to the destination the File.
print $ds $line;
$matches++;
}
}
close $ds;
close $sr;
print "\n";
print "Macthes Found = ". $matches. "\n";
print "Reading from File = " . $source . "\n";
print "Writing it to File = " . $destination . "\n";
print "\n";
# End of file extract
#//usr/bin/perl
#将Aurgument值保存到从命令行发送的局部变量
$source=$ARGV[0];
$pattren=$ARGV[1];
$destination=$ARGV[2];
$matches=0;
#使用open函数打开文件,指针将保存到变量$fh
#与消息一起使用的模具条件是命令行提供的文件名
#不存在
打开($sr,,$destination);
#使用While循环条件,只要我们获得每行的数据。
而($line=){#在从日志中一次读取一行时。。。。
如果($line=~m/$pattern/i){
#将源文件的加工线内容打印到目标文件。
打印$ds$行;
$matches++;
}
}
收盘价$ds;
收盘价$sr;
打印“\n”;
打印“找到的Macthes=”$比赛。“\n”;
打印“从文件读取=”$来源。“\n”;
打印“将其写入文件=”$目的地。“\n”;
打印“\n”;
#文件结束提取
那么你的正则表达式可能是
\b\d{2}-\d{3}-\d{4}\b
因此,使用以下命令运行perl脚本
test.pl infile '\b\d{2}-\d{3}-\d{4}\b' outfile
那么你的正则表达式可能是
\b\d{2}-\d{3}-\d{4}\b
因此,使用以下命令运行perl脚本
test.pl infile '\b\d{2}-\d{3}-\d{4}\b' outfile
那么你的正则表达式可能是
\b\d{2}-\d{3}-\d{4}\b
因此,使用以下命令运行perl脚本
test.pl infile '\b\d{2}-\d{3}-\d{4}\b' outfile
那么你的正则表达式可能是
\b\d{2}-\d{3}-\d{4}\b
因此,使用以下命令运行perl脚本
test.pl infile '\b\d{2}-\d{3}-\d{4}\b' outfile
$pattren=12-345-6789,所以从文件中读取,如果模式在任何一行中匹配,它应该打印该行,我希望我是有意义的:(@HaiderRehman我没听懂你的意思。这个数字可以在这行的任何地方吗?那行只有一个数字?你能发布示例输入文件吗?@HaiderRehman你的代码采用任何正则表达式模式并与之匹配。所以你所要做的就是:
perl yourcode.pl infle.txt'^\d{2}-\d{3}-\d{4}$'outfile.txt
。outfile中的所有数字都与您想要的模式匹配。@gideon它似乎还包含一些其他带有数字的文本。将^
和$
替换为\b
@jkshah好主意:)不过OP应该显示他的输入文件。$pattren=12-345-6789,所以从文件中读取,如果模式在任何一行中匹配,它应该打印该行,我希望我是有意义的:(@HaiderRehman我没听懂你的意思。这个数字可以在这行的任何地方吗?那行只有一个数字?你能发布示例输入文件吗?@HaiderRehman你的代码采用任何正则表达式模式并与之匹配。所以你所要做的就是:perl yourcode.pl infle.txt'^\d{2}-\d{3}-\d{4}$'outfile.txt
。outfile中的所有数字都与您想要的模式匹配。@gideon它似乎还包含一些其他带有数字的文本。将^
和$
替换为\b
@jkshah好主意:)不过OP应该显示他的输入文件。$pattren=12-345-6789,所以从文件中读取,如果模式在任何一行中匹配,它应该打印该行,我希望我是有意义的:(@HaiderRehman我没听懂你的意思。这个数字可以在这行的任何地方吗?那行只有一个数字?你能发布示例输入文件吗?@HaiderRehman你的代码采用任何正则表达式模式并与之匹配。所以你所要做的就是:perl yourcode.pl infle.txt'^\d{2}-\d{3}-\d{4}$'outfile.txt
。outfile中的所有数字都与您想要的模式匹配。@gideon它似乎还包含一些其他带有数字的文本。将^
和$
替换为\b
@jkshah好主意:)不过OP应该显示他的输入文件。$pattren=12-345-6789,所以从文件中读取,如果模式在任何一行中匹配,它应该打印该行,我希望我是有意义的:(@HaiderRehman我没听懂你的意思。这个数字可以在这行的任何地方吗?那行只有一个数字?你能发布示例输入文件吗?@HaiderRehman你的代码采用任何正则表达式模式并与之匹配。所以你所要做的就是:perl yourcode.pl infle.txt'^\d{2}-\d{3}-\d{4}$'outfile.txt
。outfile有所有与您想要的模式匹配的数字。@gideon似乎这行还包含一些其他带有数字的文本。用^
和$
替换为\b
@jkshah good thinking:)OP应该显示他的输入文件。虽然是旁注,但您似乎在编写Perl 4风格的Perl=).使用strict;使用warnings
并使用my
声明具有作用域的变量。尽管是旁注,您似乎在编写Perl 4样式Perl=)。使用strict;使用warnings
并使用my
声明具有作用域的变量。尽管是旁注,您似乎在编写Perl 4样式Perl=).使用strict;使用warnings
并使用my
声明具有作用域的变量。尽管有一个旁注,您似乎在编写Perl 4风格的Perl=)<代码>使用严格;使用警告并使用my