gpfei / python-pcre2

A simple Python wrapper of PCRE2
MIT License
6 stars 1 forks source link

Cannot get matches. only got groups. #3

Open Wrufesh opened 3 years ago

Wrufesh commented 3 years ago

I was trying to search with PCRE2 regex.

import pcre2 p = pcre2.PCRE2(r"\w+\(\s*('[^'\\]*'|\d+(?:\.\d*)?|\w+(?:\(\w*\))?)(?:\s*[,+\/*-]\s*(?1))*\s*\)".encode()) string = "5 + __FXN1__(hello, 1, 3, '__HELLO__(\"hello\") + 5') + 5 + (2/2) + __FXN2__('Good boy')" result = p.search(string.encode()) result.groups()

Result: [b'hello']

I only got groups as result. I need matches.

Here is what I want: https://regex101.com/r/I7EPSs/1/

gpfei commented 3 years ago

Sorry, I turned to Golang 5 years ago.

You could fork and add this feature like following:

https://github.com/gpfei/python-pcre2/blob/master/src/pcre2.pyx#L171

cdef class Match:
    ...

cdef Result ResultFactory(...):
    ...
    for i in range(match_count):
        substring = content[ovector[2*i]:ovector[2*i+1]]
        # construct "Match" instance here
        #    cdef m Match
        res.add_match(m)  # need to redefine `add_match` or define another one.
    return res