如何在 Perl 中记住匹配项及其在数组中的位置?
请帮助
我正在处理一个文件,其数据行如下所示。 可以看到,数据被'|||
'分成了4份,所以我将有四个数组(如果我划分它)。 我想要的是这样的:
- 我想检查第一个数组中是否有标点符号,如果有,记住数组中的位置。
- 转到第三个数组中的相同位置,并读取括号中的数字。
- 检查数字数组索引处的值是否为标点符号。
我的问题是我不记得比赛及其位置! 你能帮忙吗?
útil por la unión europea , a ||| by the european union , ||| () (0) (1) (3) (2) (4) () ||| (1) (2) (4) (3) (5)
Please help
I am working with a file whose lines of data look like the one below. As can be seen, the data is divided into 4 by '|||
', so I will have four arrays( if I divide it). what I want is this:
- I want to check if there are punctuation marks in the first array, if there is one, remember the position in the array.
- Go to the same position in the third array, and read the number in the bracket.
- Check if the value at the array index of the number is punctuation.
My problem is that I could not remember the match and its position! Can you help here, please?
útil por la unión europea , a ||| by the european union , ||| () (0) (1) (3) (2) (4) () ||| (1) (2) (4) (3) (5)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了
pos()
之外,还有@-
和@+
:In addition to
pos()
, there are@-
and@+
:pos()
函数可用于报告比赛的(结束)位置。 示例:此代码将打印“There is an 'e'结尾于位置 5。” (位置从 0 开始。)将此与捕获括号的正常使用结合起来,您应该能够解决您的问题。
除了
pos()
之外,还有特殊全局数组@-
和@+
提供每个子模式的开始和结束偏移量匹配。 示例:(感谢 Chas. Owens 唤起我的记忆;我正在查看
perlre
为他们而不是在perlvar
< /a>)The
pos()
function can be used to report the (ending) position of a match. Example:This code will print, "There is an 'e' ending at position 5." (Positions start from 0.) Combine this with the normal use of capturing parentheses and you should be able to solve your problem.
In addition to
pos()
, there are also the special global arrays@-
and@+
which provide the start and end offsets of each subpattern matched. Example:( Thanks to Chas. Owens for jogging my memory on these; I was looking in
perlre
for them instead of inperlvar
)当您需要在代码中完成一些并不简单的事情时,最好将其分解为离散的步骤和变量,以便于理解。
所以我首先将数据字符串分成四个部分:
类似的东西......
When you have something to do something in code that isn't simple, it's best to break it down into discrete steps and variables so that it is easy to understand.
So I would first split the data string into it's four parts:
Something like that ...