简体   繁体   English

格式化perl正则表达式捕获组

[英]Format perl regex capture groups

Consider the following situation. 考虑以下情况。 It is hypothetical, but demonstrates a general thing I would like accomplished. 这是假设的,但展示了我想要完成的一般事情。

Suppose I have a file, which has one or more lines. 假设我有一个文件,它有一行或多行。 On each line, it may have one or more instances of [name] = [value] , where [name] is some variable name and [value] is some value. 在每一行上,它可能有一个或多个[name] = [value]实例,其中[name]是一些变量名, [value]是某个值。 Suppose further that each of these are matched by /[a-zA-Z]+=[0-9]+/ . 进一步假设这些中的每一个都匹配/[a-zA-Z]+=[0-9]+/

What I would like is a perl expression that will print out each match, formatted in a particular way. 我想要的是一个perl表达式,它将打印出每个匹配,以特定方式格式化。 My intent is to use this on the command line to parse data from files. 我的意图是在命令行上使用它来解析文件中的数据。 A hypothetical solution, made invalid because perl doesn't actually accept this syntax: print m/([a-zA-Z]+)=([0-9]+)/name: \\1, value: \\2\\n/g , which, when run on each line, in our ideal world, would print out each match from each line like name:[name], value:[value] , each formatted match on its own line. 一个假设的解决方案,因为perl实际上不接受这种语法而无效: print m/([a-zA-Z]+)=([0-9]+)/name: \\1, value: \\2\\n/g ,当在每一行上运行时,在我们理想的世界中,将打印每行的每个匹配,如name:[name], value:[value] ,每个格式化的匹配在其自己的行上。

For example, consider this input file test.txt: 例如,考虑这个输入文件test.txt:

blah blah count=5 blah i=1
books=2 blah
blah fairies=87 water=0

Suppose we then type our magic command into bash, something like the following: 假设我们然后将我们的魔术命令输入bash,如下所示:

perl -n -e 'print m/([a-zA-Z]+)=([0-9]+)/name: \1, value: \2\n/g' test.txt

(It might be more reasonable to require some kind of loop over all returned matches, but hopefully you get the idea.) (对所有返回的匹配要求某种循环可能更合理,但希望你能得到这个想法。)

It would print the following: 它将打印以下内容:

name: count, value: 5
name: i, value: 1
name: books, value: 2
name: fairies, value: 87
name: water, value: 0

I realize that this syntax does not actually work, but I would like to accomplish the same thing in as brief a piece of perl as possible. 我意识到这种语法实际上并不起作用,但我希望尽可能简单地完成同样的事情。 I hope to be able to use it occasionally on the command line to find and format text. 我希望能够偶尔在命令行中使用它来查找和格式化文本。 I've written my own ruby script, but its a bit buggy, and not included in a standard environment (or in anybody's environment but my own). 我编写了自己的ruby脚本,但它有点儿麻烦,并没有包含在标准环境中(或者在任何人的环境中,但我自己的)。 Anybody know some perl secrets? 有人知道一些perl的秘密吗?

You were pretty close. 你非常接近。 ;-) ;-)

$ perl -ne 'print "name: $1, value: $2\n" while /([a-zA-Z]+)=([0-9]+)/g;' test.txt
name: count, value: 5
name: i, value: 1
name: books, value: 2
name: fairies, value: 87
name: water, value: 0

Edit: since your comment seemed to indicate that shorter is better, here's a version with a few characters shaved off: 编辑:因为你的评论似乎表明更短是更好,这里是一个削减了几个字符的版本:

$ perl -lne 'print "name: $1, value: $2" while /([A-Z]+)=(\d+)/gi' test.txt

My suggestion would be - consider selecting your vars into a hash. 我的建议是 - 考虑选择你的变量为哈希。

use Data::Dumper;
local $/;
my %stuff = <> =~ m/(\w+)=(\d+)/g;
print Dumper \%stuff ;

Should do approximately what you want. 应该大概做你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM