banetl banetl - 5 months ago 42
Python Question

Convert a python regexp to java

I know it has already been asked Here however I would like to use named group in java like Pshemo suggested and I can't figure what's wrong with my regex conversion:

Here is the python regexp:

regexp = re.compile(r'(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)', re.DOTALL | re.MULTILINE)
matches = regexp.findall(content)

Here is my java converted regexp:

String regexp = "(?<delim>[^\\w\\n\\\"'])(?<space> ?)(?<quote>[\\\"']).*?(?=quote)(?=delim)";
Pattern pattern = Pattern.compile(regexp, Pattern.DOTALL | Pattern.MULTILINE);
Matcher matcher = pattern.matcher(content);

What I am doing wrong ?


You converted named backreferences into positive lookaheads ((?P=quote) => (?=quote)), while you need \k<groupname>:

String regex = "(?<delim>[^\\w\n\"'])(?<space> ?)(?<quote>[\"']).*?\\k<quote>\\k<delim>";

Tested at OCPSoft regex tester:

enter image description here