提取方括号"[]"之间的数据使用Perl [英] Extract data between square brackets "[]" using Perl
问题描述
我正在使用正则表达式从弧形括号(或括号")中提取数据,就像从(a,b)
中提取a,b
一样,如下所示.我有一个文件,其中每一行都会像
I was using a regex for extracting data from curved brackets (or "parentheses") like extracting a,b
from (a,b)
as shown below. I have a file in which every line will be like
this is the range of values (a1,b1) and [b1|a1]
this is the range of values (a2,b2) and [b2|a2]
this is the range of values (a3,b3) and [b3|a3]
我正在使用以下字符串提取a1,b1
,a2,b2
等...
I'm using the following string to extract a1,b1
, a2,b2
, etc...
@numbers = $_ =~ /\((.*),(.*)\)/
但是,如果我想从方括号[]
中提取数据,该怎么办?例如
However, if I want to extract the data from square brackets []
, how can I do it? For example
this is the range of values (a1,b1) and [b1|a1]
this is the range of values (a1,b1) and [b2|a2]
我只需要提取/匹配方括号中的数据,而不提取/匹配括号中的数据.
I need to extract/match only the data in square brackets and not the curved brackets.
推荐答案
[更新] 同时,我写了一篇有关.*
特定问题的博客文章,如下所述: 为什么在正则表达式中几乎不使用.*
[Update] In the meantime, I've written a blog post about the specific issue with .*
I describe below: Why Using .* in Regular Expressions Is Almost Never What You Actually Want
如果标识符a1
,b1
等本身不包含逗号或方括号,则应按照以下方式使用模式,以避免回溯地狱:
If your identifiers a1
, b1
etc. never contain commas or square brackets themselves, you should use a pattern along the lines of the following to avoid backtracking hell:
/\[([^,\]]+),([^,\]]+)\]/
这是Regex101上的工作示例.
Here's a working example on Regex101.
像.*
这样的贪婪量词的问题在于,一开始您很可能会消耗过多,因此正则表达式引擎必须进行大量的回溯.即使您使用非贪婪的量词,引擎也会进行比实际需要更多的匹配尝试,因为它一次只消耗一个字符,然后尝试提升模式中的位置.
The issue with greedy quantifiers like .*
is that you'll very likely consume too much in the beginning so that the regex engine has to do extensive backtracking. Even if you use non-greedy quantifiers, the engine will do more attempts to match than necessary because it'll only consume one character at a time and then try to advance the position in the pattern.
(您甚至可以使用原子组使匹配更加有效.)
(You could even use atomic groups to make the matching even more performant.)
这篇关于提取方括号"[]"之间的数据使用Perl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!