如何在正则表达式中组合正负条件?
我对正则表达式相当陌生,需要一些帮助。我需要在 Perl 中使用正则表达式过滤一些行。我将把正则表达式传递给另一个函数,因此它需要在一行中完成。
我只想选择包含 "too long"
且不以 "SKIPPING"
开头的行,
这是我的测试字符串:
由于到期时间太长而跳过该债券
TKIPPING 该债券到期时间过长
由于到期时间过长而对该债券进行拍打
你好,这个成熟期太长了
这太长了
你好
表达式规则应与“太长”匹配以下内容:
由于到期时间过长而跳过该债券
由于到期时间过长而对该债券进行拍打
你好,这个成熟期太长了
这太长了
,应该跳过:
“你好”,因为它不包含“太长”
“由于到期太久而跳过此债券”,因为它包含“跳过”
I fairly new to regular expressions and need some help. I need to filter some lines using regex in Perl. I am going to pass the regex to another function so it needs to be done in a single line.
I want to select only lines that contain "too long"
and that don't begin with "SKIPPING"
Here are my test strings:
SKIPPING this bond since maturity too long
TKIPPING this bond since maturity too long
SLAPPING this bond since maturity too long
Hello this maturity too long
this is too long
hello there
The regex rule should match the following on 'too long":
SKIPPING this bond since maturity too long
SLAPPING this bond since maturity too long
Hello this maturity too long
this is too long
and it should skip:
"hello there" because it doesn't contain 'too long'
"SKIPPING this bond since maturity too long" because it containst 'SKIPPING'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
就我个人而言,我会将其作为两个单独的正则表达式来执行,只是为了使其更清晰。
Personally, I'd do this as two separate regex just to make it clearer.
我怀疑你可能在一个正则表达式之后,但我更喜欢分成更具可读性的东西,如下所示:
I suspect you maybe after a single regex however I prefer to split into something more readable like this:
使用前瞻;请参阅正则表达式环视的说明。
Use a lookahead; see this explanation of regex lookaround.
匹配您正在寻找的线路。末尾的美元符号导致它仅匹配以“too long”结尾的字符串。
希望这有帮助!
Matches the lines you're looking for. The dollar sign at the end causes it to match only strings that end with "too long".
Hope this helps!
使用负向后查找:
Using negative lookbehind: