使用 Perl 正则表达式返回子字符串
我正在使用 Perl 并试图更好地理解它的子字符串/正则表达式功能。
假设我有一个字符串,例如
[48:31.8] Sent: >33*1311875297587*eval*0*frame[0]*"A"<
并想返回 1311875297587。它将始终采用该格式。我该如何使用 Perl 来做到这一点?
谢谢
I'm playing around with Perl and trying to get a better understanding of its substring/regex functionality.
Say I have a string such as
[48:31.8] Sent: >33*1311875297587*eval*0*frame[0]*"A"<
and want to return 1311875297587. It will always be in that format. How would I do this using Perl?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设“[48:31.8]...”在$string中,那么:
如果字符串不匹配,
$number
将是未定义的,否则它将包含”之间的数字*”
和“*eval*”
。Assuming that "[48:31.8]..." is in $string, then:
$number
will be undefined if the string doesn't match, otherwise it will contain the digits between"*"
and"*eval*"
.就其价值而言,我认为这是一个更可靠的答案:
如果您需要的话,这允许访问所有字段,并且不依赖于固有顺序来解析特定字段。
将
while
循环替换为您想要的任何逐行迭代。不过,为了进行测试,请将其作为 Perl 脚本运行,然后粘贴到您的行[48:31.8] Sent: >33*1311875297587*eval*0*frame[0]*"A"<
或其他什么。For what it's worth, I think this is a more robust answer:
This allows access to all of your fields if you need them, and doesn't rely on inherent order to parse out a particular field.
Replace the
while
loop with whatever line-by-line iteration you want. For testing though, run this as a Perl script, then paste in your line[48:31.8] Sent: >33*1311875297587*eval*0*frame[0]*"A"<
or whatever else.