Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improvements in regular expression doc#114357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
adorilson wants to merge34 commits intopython:main
base:main
Choose a base branch
Loading
fromadorilson:re_improvements

Conversation

adorilson
Copy link
Contributor

@adorilsonadorilson commentedJan 20, 2024
edited by github-actionsbot
Loading

adorilsonand others added6 commitsJanuary 20, 2024 20:23
The check about the f argument type was removed in this commit:python@2c94aa5Thanks for Pedro Arthur Duarte (pedroarthur.jedi at gmail.com) by the help withthis bug.
…#106335)Remove private _PyThreadState and _PyInterpreterState C APIfunctions: move them to the internal C API (pycore_pystate.h andpycore_interp.h). Don't export most of these functions anymore, butstill export functions used by tests.Remove _PyThreadState_Prealloc() and _PyThreadState_Init() from the CAPI, but keep it in the stable API.
@bedevere-appbedevere-appbot added awaiting review docsDocumentation in the Doc dir skip news labelsJan 20, 2024
Copy link
Member

@terryjreedyterryjreedy left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This PR does 3 things.

  1. Add headers. I have thought to propose the same. Please add 1 more at 320, something like
.. _re_extension_notationExtension notation^^^^^^^^^^^^^^^^^^

CHANGE

  1. Add double backticks, either new or extending single backticks. The existing text always put backticks on REs and sometimes on text matched. PR makes that (nearly, 2 expections noted) always on matches. Defensible since this seems the majority of existing cases. CHANGE

  2. Add 'only' in several places. I am not sure these are needed, but I see existing similar uses.

@serhiy-storchaka I want to finish this RE doc change. Any additional comments from you?

Comment on lines 124 to 125
only 'foo'. More interestingly, searching for ``foo.$`` in ``'foo1\nfoo2\n'``
matches 'foo2' normally, but 'foo1' in :const:`MULTILINE` mode; searching for
matches 'foo2' normally, but ``'foo1'`` in :const:`MULTILINE` mode; searching
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

To be consistent with other additions, 'foo' above and 'foo2' here should be backticked. But see review summary.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Done.

@bedevere-app
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phraseI have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@serhiy-storchaka
Copy link
Member

I am not sure that there is a need in these changes.

  1. New headers and anchors. It is perhaps harmless, but there are some problems in the text (re: documentation claim that special characters lose their special meaning inside […] seems wrong #106482) which requires more serious rewriting, so some parts of the text can be moved and headers and anchors can change.
  2. I used double backquotes to highlight regular expressions. Single quotes are used for strings, they are not fragments of the Python code, they are just strings in quotes. If use the same style in both cases, it will be more difficult to distinguish REs from strings. Maybe you have better solution?
  3. I have no opinion about "only", I left the decision on the native English users.

@adorilsonadorilson marked this pull request as draftSeptember 25, 2024 09:45
@adorilson
Copy link
ContributorAuthor

Hi,@terryjreedy. Thank you for your review and comments.

The items 1 and 2 are done.

Concern 3: the idea is to make there.ASCII use more explicit.

Withoutre.ASCII,[^0-9] is matched, but something more can be matched too, i.e.:

>>>importre>>>re.findall(r'\d+','567abc123٠١٢٣٤٥٦٧٨٩')['567','123٠١٢٣٤٥٦٧٨٩']

However, withre.ASCII only (and just only)[^0-9] is matched, i.e.:

>>>importre>>>re.findall(r'\d+','567abc123٠١٢٣٤٥٦٧٨٩',re.ASCII)['567','123']

@adorilson
Copy link
ContributorAuthor

  1. requires more serious rewriting

This can start adding more in-line examples, like in progress with strings (#119445).

@bedevere-app
Copy link

Thanks for making the requested changes!

@terryjreedy: please review the changes made to this pull request.

Copy link
Member

@vadmiumvadmium left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I appreciate the subheadings to divide up the syntax section.

I agree with addingonly to clarify when ASCII mode matches less than previously described. But the cases with complemented sets don’t have this problem, and I think addingonly to them only hurts.

\D:
Matches any character which is not a decimal digit. This is the opposite of \d.

Matches only [^0–9] if the ASCII flag is used.

reads as “Matches only the universe except zero to nine”?

@@ -514,6 +529,9 @@ The special characters are:

.. _re-special-sequences:

Special sequences
^^^^^^^^^^^^^^^^^
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Can we call them all escape sequences? Differentiates better from the multi-character “special character” sequences above.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Are there other subtypes of special characters? What if about subsections?

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I suggest change the next heading to something likeString literal escapes, and change this heading fromSpecial sequences toEscape sequences.

These are the types of the special characters I can think of for REs:

  • The single-character metacharacters:$, *, [, ], \, etc, as listed in the how-tohttps://cpython-previews--114357.org.readthedocs.build/en/114357/howto/regex.html#matching-characters
  • Multicharacter syntax built with the metacharacters, like *?, {m,n} and the bracketed extension notation (?. . .)
  • “Special sequences” a.k.a. escape sequences, which begin with a backslash. These could be subdivided into
    • Non-alphanumeric, for escaping metacharacters and other syntax:\$, \*, \\, \', \", etc
    • Group references \1–\99
    • Alphanumeric sequences that specify locations to match, or categories of characters: \A, \b, \d, etc
    • String literal escapes:\n, \\, \N{. . .}, \0–\777, etc. Excludes \b and\<newline>.
  • Characters only special in “verbose” expressions: whitespace and #
  • Additional backslash sequence forre.sub templates: \g<. . .>
  • Special characters inside square-bracketed classes/sets [. . .], especially -, ^, ], \b, and reserved [, &&, etc

matches both ``'foo'`` and ``'foobar'``, while the regular expression ``foo$``
matches
only ``'foo'``. More interestingly, searching for ``foo.$`` in ``'foo1\nfoo2\n'``
matches ``'foo2'`` normally, but also ``'foo1'`` in :const:`MULTILINE` mode; searching
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I thought the original was easier to read, with the full string being searched given in a different font from the substrings that are found

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Firstly, it was inconsistent with the "(In the rest of this section, we’ll write RE’s in this special style, usually without quotes, and strings to be matched 'in single quotes'.)"

However, you highlighted that 'strings to be matched' is different from 'the matches'. On the other hand, both are literal strings, and this is a common pattern around all docs.

I would like some more opinions here.

many repetitions as are possible. ``ab*`` will match 'a','ab', or 'a' followed
by any number of 'b's.
many repetitions as are possible. ``ab*`` will match``'a'``, ``'ab'``, or
``'a'`` followedby any number of``'b'``s.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The -s signifying plural has become disconnected from theb

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Waiting decision about:#114357 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Not that I think it is great formatting, but looking at the markup under *+ you might join thes on with

Suggested change
``'a'`` followed by any number of ``'b'`` s.
``'a'`` followed by any number of ``'b'``\s.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Done


.. index:: single: + (plus); in regular expressions

``+``
Causes the resulting RE to match 1 or more repetitions of the preceding RE.
``ab+`` will match 'a' followed by any non-zero number of 'b's; it will not
match just 'a'.
``ab+`` will match ``'a'`` followed by any non-zero number of ``'b'`` s; it
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

-s disconnected again

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Done

@adorilson
Copy link
ContributorAuthor

I agree with addingonly to clarify when ASCII mode matches less than previously described. But the cases with complemented sets don’t have this problem, and I think addingonly to them only hurts.

I've reverted this change.

\D:
Matches any character which is not a decimal digit. This is the opposite of \d.
Matches only [^0–9] if the ASCII flag is used.

reads as “Matches only the universe except zero to nine”?

HUmm... It is curious. Even without theonly, this can sound weird, mainly if you don't read the first paragraph.

Maybe this would be "Matches all the ASCII universe except zero to nine."

@adorilson
Copy link
ContributorAuthor

I have made the requested changes; please review again

@bedevere-app
Copy link

Thanks for making the requested changes!

@terryjreedy: please review the changes made to this pull request.

@adorilson
Copy link
ContributorAuthor

I have made the requested changes; please review again

@bedevere-app
Copy link

Thanks for making the requested changes!

@terryjreedy: please review the changes made to this pull request.

@vadmium
Copy link
Member

\D:
Matches any character which is not a decimal digit. This is the opposite of \d.

Matches [^0–9] if the ASCII flag is used.

HUmm... It is curious. Even without theonly, this can sound weird, mainly if you don't read the first paragraph.

Maybe this would be "Matches all the ASCII universe except zero to nine."

I don’t understand what is weird. It matches all Unicode characters, not just ASCII, except for ASCII zero to nine. The only suggestion I can think of is saying “Equivalent to [^0–9]” rather than “Matches”. Maybe that is clearer to you? (Although the equivalency doesn’t quite work when \D is already inside a square-bracket character class/set.)

@adorilson
Copy link
ContributorAuthor

\D: Matches any character which is not a decimal digit. This is the opposite of \d.

Matches [^0–9] if the ASCII flag is used.

HUmm... It is curious. Even without theonly, this can sound weird, mainly if you don't read the first paragraph.
Maybe this would be "Matches all the ASCII universe except zero to nine."

I don’t understand what is weird. It matches all Unicode characters, not just ASCII, except for ASCII zero to nine. The only suggestion I can think of is saying “Equivalent to [^0–9]” rather than “Matches”. Maybe that is clearer to you? (Although the equivalency doesn’t quite work when \D is already inside a square-bracket character class/set.)

Oh, my goodness.

I had a misconception about how re.ASCII works. I thought it was like a filter: "filter all ASCII characters and after matching against the re". So, necessarily, with the ASCII flag, the matches only had ASCII characters. But it is not true, especially with the negative set of characters ([^ re ]).

In this case, we might need to improve there.ASCII definition to avoid this misconception.

@adorilson
Copy link
ContributorAuthor

The only suggestion I can think of is saying “Equivalent to [^0–9]” rather than “Matches”. Maybe that is clearer to you?

Yeah. With the correct understanding of how ASCII works, it sounds better.

Can I change this in all occurrences like that?

@willingc
Copy link
Contributor

Thank you@adorilson for your patience and effort on this PR.

@terryjreedy I'm going through and triaging a bunch of docs PRs. If you have time, please review this one again. Thanks.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@terryjreedyterryjreedyAwaiting requested review from terryjreedy

@vadmiumvadmiumAwaiting requested review from vadmium

Assignees
No one assigned
Labels
Projects
Status: Todo
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@adorilson@serhiy-storchaka@vadmium@willingc@terryjreedy@vstinner

[8]ページ先頭

©2009-2025 Movatter.jp