Perl:模式匹配字符串然后打印下一行
我正在使用 Net::Whois::Raw 从文本文件中查询域列表,然后对其进行解析以输出每个域的相关信息。
一切都很顺利,直到我达到 Nominet 结果,因为我需要的信息永远不会与我正在模式匹配的信息在同一行。
例如:
名称服务器:
ns.mistral.co.uk 195.184.229.229
所以我需要做的是“名称服务器:”的模式匹配,然后显示下一行或多行,但我无法管理它。
我已经阅读了这里的所有答案,但它们要么似乎不适用于我的情况,要么让我更加困惑,因为我只是一只简单的熊。
我使用的代码如下:
while ($record = <DOMAINS>) {
$domaininfo = whois($record);
if ($domaininfo=~ m/Name servers:(.*?)\n/){
print "Nameserver: $1\n";
}
}
我尝试了一个 Stackoverflow 的示例,其中
<DOMAINS>;
将采用下一行,但这对我不起作用,我认为这是因为我们已经将其内容读入 $domaininfo 中。
编辑:忘了说谢谢! 多么粗鲁。
I am using Net::Whois::Raw to query a list of domains from a text file and then parse through this to output relevant information for each domain.
It was all going well until I hit Nominet results as the information I require is never on the same line as that which I am pattern matching.
For instance:
Name servers:
ns.mistral.co.uk 195.184.229.229
So what I need to do is pattern match for "Name servers:" and then display the next line or lines but I just can't manage it.
I have read through all of the answers on here but they either don't seem to work in my case or confuse me even further as I am a simple bear.
The code I am using is as follows:
while ($record = <DOMAINS>) {
$domaininfo = whois($record);
if ($domaininfo=~ m/Name servers:(.*?)\n/){
print "Nameserver: $1\n";
}
}
I have tried an example of Stackoverflow where
<DOMAINS>;
will take the next line but this didn't work for me and I assume it is because we have already read the contents of this into $domaininfo.
EDIT: Forgot to say thanks!
how rude.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,$domaininfo 字符串包含您的域?
您可能需要的是正则表达式末尾的
m
参数。这会将您的字符串视为多行字符串(事实就是如此)。然后,您可以匹配\n
字符。这对我有用:现在,我可以匹配
Nameservers:
行末尾的\n
并捕获下一行的名称和 IP 地址。这可能需要稍微修改一下才能使其在您的情况下工作。
So, the $domaininfo string contains your domain?
What you probably need is the
m
parameter at the end of your regular expression. This treats your string as a multilined string (which is what it is). Then, you can match on the\n
character. This works for me:Now, I can match the
\n
at the end of theName servers:
line and capture the name and IP address which is on the next line.This might have to be munged a bit to get it to work in your situation.
这是一半的问题,也许是一半的答案(问题在这里,因为我还没有被允许写评论......)。好的,我们开始吧:
这是您正在解析的文件中的条目的样子吗?接下来会发生什么——更多的域名和IP地址?中间会有空行吗?
不管怎样,我认为你的问题可能(部分?)与你逐行读取文件有关。一旦您到达 IP 地址行,有关“名称服务器:”的信息就会消失。如果您逐行查看文件,多行匹配将无济于事。因此我建议切换到段落模式:
但是你说
那么也许您已经尝试过我刚才建议的方法?另一种方法是添加另一个变量($indicator 或其他变量),一旦读取“名称服务器:”,您将其设置为 1,并且只要它等于 1,所有后续行都将被视为包含数据你需要。然而,这是否可行取决于您始终了解数据文件还包含哪些内容。
我希望这里的内容对您有所帮助。如果有任何疑问,请询问:)
This is half a question and perhaps half an answer (the question's in here as I am not yet allowed to write comments...). Okay, here we go:
Is this what an entry in the file you're parsing looks like? What will follow immediately afterwards - more domain names and IP addresses? And will there be blank lines in between?
Anyway, I think your problem may (in part?) be related to your reading the file line by line. Once you get to the IP address line, the info about 'Name servers:' having been present will be gone. Multiline matching will not help if you're looking at your file line by line. Thus I'd recommend switching to paragraph mode:
But then you said
so maybe you've already tried what I have just suggested? An alternative would be to just add another variable ($indicator or whatever) which you'll set to 1 once 'Name servers:' has been read, and as long as it's equal to 1 all following lines will be treated as containing the data you need. Whether this is feasible, however, depends on you always knowing what else your data file contains.
I hope something in here has been helpful to you. If there are any questions, please ask :)