本文介绍了Java正则表达式带有积极面貌,背后是负面展望的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从这种字符串中提取 ou =人,ou =(。*),dc = company,dc = org 最后一个字符串前面有昏迷没有后跟(。*)。在最后一种情况下,这应该给 dc = company,dc = org

I am trying to extract from this kind of string ou=persons,ou=(.*),dc=company,dc=org the last string immediately preceded by a coma not followed by (.*). In the last case, this should give dc=company,dc=org.

看看正则表达式,这似乎成为负面展望背后(前面)的正面看法。

Looking on regex, this seems to be a positive look behind (preceded by) of a negative look ahead.

所以我实现了这个正则表达式:(?< =( ,(?!。* \ Q(。*)\ E)))。* ,但它返回,dc = company,dc = org 昏迷。我想要没有昏迷的同样的事情。我做错了什么?

So I have achieve this regex: (?<=(,(?!.*\Q(.*)\E))).*, but it returns ,dc=company,dc=org with the coma. I want the same thing without the coma. What I am doing wrong?

推荐答案

似乎我单独解决了我的问题,取消了前方负面看法周围的捕获组。它给出了以下正则表达式:(?< =,(?!。* \ Q(。*)\ E))。*

It seems that I have solved my problem alone, removing the capturing group around the negative look ahead. It gives the following regex: (?<=,(?!.*\Q(.*)\E)).*.

它与周围捕获组的行为相关联,如下所述:部分 Lookaround Is Atomic

It is linked with the behavior of capturing groups in look arounds as explained here: http://www.regular-expressions.info/lookaround.html in the part Lookaround Is Atomic.

这篇关于Java正则表达式带有积极面貌,背后是负面展望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:49