我有路由器命令的输出:
Interface IP-Address OK? Method Status Protocol
POS0/0/0 10.137.99.2 YES NVRAM up up
Run Code Online (Sandbox Code Playgroud)
我想找一个正则表达式来识别IP地址:
我尝试过:
if ( $_ =~ m/(.*?)\s*?(.*?)\s*?(.*?)\s*?(.*)/i ){
#print "$1->$2\n";
$sources{$2}=$1;
}
Run Code Online (Sandbox Code Playgroud)
然后$2用作ip.
但是,它不起作用; 我哪里错了?
我曾经看过一个正则表达式的例子
^\s*(+-)?(\d+|\d*\.\d+|\d+\.\d*)(e?(+-)?......$
Run Code Online (Sandbox Code Playgroud)
但我只是不知道如何理解/分析这一个?
我是Perl的新手,我试图使用Strawberry perl 5,版本16执行下面编码的简单程序:
#!usr/bin/perl
use warnings;
use strict;
my @array= {1,2,3,5,7,9};
my $i;
foreach $i (@array)
{
print qq(Element is $i\n);
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
Element is HASH(0x3f8b4c)
Run Code Online (Sandbox Code Playgroud)
但是我应该收到的输出是:
Element is 1
Element is 2
Element is 3
Element is 5
Element is 7
Element is 9.
Run Code Online (Sandbox Code Playgroud)
感谢你的帮助.
我使用XML :: Simple来解析XML文件.输出是一个哈希值.(使用Data :: Dumper)
示例代码,XML文件在下面给出了输出.
Perl代码:
use XML::Simple;
use Data::Dumper;
$xml = new XML::Simple;
$data = $xml->XMLin("spam.xml");
print Dumper($data);
Run Code Online (Sandbox Code Playgroud)
XML文件内容(输入到解析器)::
<Attach_request>
<Protocol_discriminator>
<name> Protocol_discriminator </name>
<attribute> Mandatory </attribute>
<type> nibble </type>
<value> 7 </value>
<min> 0 </min>
<max> F </max>
</Protocol_discriminator>
<Security_header>
<name> Security_header </name>
<attribute> Mandatory </attribute>
<type> nibble </type>
<value> 7 </value>
<min> 0 </min>
<max> F </max>
</Security_header>
<Security_header1>
<name> Security_header </name>
<attribute> Mandatory </attribute>
<type> nibble </type>
<value> 7 </value>
<min> 0 </min>
<max> F …Run Code Online (Sandbox Code Playgroud) 我希望以下列方式为大型多行文本进行匹配:
我有几个匹配的模式:
$text =~ m#finance(.*?)end#s;
$text =~ m#<class>(.*?)</class>#s;
$text =~ m#/data(.*?)<end>#s;
如果任何一个匹配,则打印结果print $1,然后继续文本的其余部分以再次匹配三个模式.
如何按照它们在整个文本中出现的顺序获得打印结果?
非常感谢您的帮助!
perl开关"-E"和"-e"如何相互不同?在这个例子中,它们的工作方式完全相同 - 在切换后执行命令:
$ perl -e 'print "$_\n" foreach 1..2'
1
2
$ perl -E 'print "$_\n" foreach 1..2'
1
2
Run Code Online (Sandbox Code Playgroud) 为什么以下代码无法进入匿名数组?
my @d = [3,5,7];
print $(@{$d[0]}[0]);
# but print $d[0][0] works.
Run Code Online (Sandbox Code Playgroud) 所以我要做的是删除所有内容,包括第一个"/"后出现在"."之后.所以:http://linux.pacific.net.au/primary.xml.gz 将成为:http://linux.pacific.net.au
我如何使用正则表达式执行此操作?我正在运行的系统无法使用URI工具.