我有以下维度元素列表(不完整,实际上要长得多):
Ktr_12345_180
Ktr_12345_160
Ktr_12345_1130
Kst_12345_180
Kst_12345_112
Kst_12345_120
Kst_12345_160
我的目标是在 Jedox 2019.1 子集编辑器中创建元素子集。 子集应包含以前缀 "Ktr_"
开头但不包含后缀 _160
或 _180
我已经构建了一个正则表达式 (Ktr_)+[0-9]+(_180|_160)
来标识我不想要的元素。
现在我必须反转它。据我所知,没有用于反转正则表达式的 native 函数,对吗?
所以我尝试通过使用负前瞻来做到这一点:(Ktr_)+[0-9]+(?!(_180|_160))
这根本不起作用。我尝试了各种形状,但没有达到我的目标......
我希望正则表达式能够提供所需的元素。相反,它只显示以 "Ktr_"
作为前缀的每个元素。
请您参考如下方法:
问题是 +
只在它服务于整个模式匹配时才是贪婪的;在[0-9]+
中,它不匹配所有后面的数字,但仅匹配所有,以便模式的其余部分也匹配 .
捕获模式的可变部分([0-9]+
)并将其打印出来查看
my @ary = qw(
Ktr_12345_180
Ktr_12345_160
Ktr_12345_1130
Kst_12345_180
);
for (@ary) {
say "got $1 in $_" if /(Ktr_[0-9]+)(?!_180|_160)/;
}
我们得到
got Ktr_1234 in Ktr_12345_180 got Ktr_12345 in Ktr_12345_1130 got Ktr_1234 in Ktr_12345_160
Matching 1234
leaves 5
to satisfy the lookahead's "not _180" as the next thing after the 1234
.
To adjust this we need data details and the question appears to allow two possibilities
If there is always a
_
following, as sample data suggests, then simply include_
before the lookahead/Ktr_[0-9]+_(?!180|160)/
现在要求
_
之前的所有数字都匹配。这也“强制”了_
那里如果我们按照文字说明进行操作
all elements starting with the prefix "Ktr_" while not having the suffix _160 or _180
那么
Ktr_12345
后面可能没有任何内容(例如),或者至少不是_
在这种情况下,仅强制匹配所有连续数字
/Ktr_[0-9]++(?!_180|_160)/
其中额外的
+
使其前面的子模式尽可能匹配,而不管整个模式中后续的内容如何;它的名称为 possesive quantifiers