1

Upon reading the python regex module, (?P<name>...) usually confused me.

I knew P here stanrds for nothing but random things as foo bar zoo from the answer python - Named regular expression group "(?Pregexp)": what does "P" stand for? - Stack Overflow

(?P<name>...)
Similar to regular parentheses, but the substring matched by the group is accessible via the symbolic group name name. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression. A symbolic group is also a numbered group, just as if the group were not named.
Named groups can be referenced in three contexts. If the pattern is (?P['"]).*?(?P=quote) (i.e. matching a string quoted with either single or double quotes):

enter image description here

Mistakes often make in repl argument in re.sub to employ \g<quote>.

Since I try to be pythonic and explain the root things to others.

why use g instead of p in \g<quote> or why not use G in (?P<name>...)?

so there will be some consistent than chaos.

AbstProcDo
  • 14,203
  • 14
  • 49
  • 94
  • I'd say it's because `\g` is always a *group*. E.g. later in the doc "\g uses the corresponding **group** number". I suppose P wasn't made for groups only originally, as discusses in the link you provided. `(?P` mentioned there in top answer was about a general *extension* to regexes. – h4z3 May 31 '19 at 10:22
  • the original regex named group is `?`, so what is the extension by adding a P @h4z3 – AbstProcDo May 31 '19 at 12:52
  • Nope. `(?` is something, Only after there's P, we know that it's an extension of this type. `(?` means nothing by itself, it can be e.g. `(?:` - non-capturing group. As I said and you linked it yourself - P was probably chosen as a general pythonic regex extension that originally was thought for more than just groups. – h4z3 May 31 '19 at 13:10
  • check regex in wikepedia @h4z3 – AbstProcDo May 31 '19 at 13:25
  • Aaand? `(?` means nothing by itself, only that something non-strictly-regex will happen. `(?<=` etc are already existing regex things (and therefore appear on wikipedia), while `(?P` means a python-specific extension - marked as such as to not collide with other regex things and extensions. – h4z3 May 31 '19 at 13:40
  • I did not follow your idea. `?` without `p` conflict with what ? @h4z3 – AbstProcDo May 31 '19 at 16:00

0 Answers0