[issue44940] Clarify the documentation of re.findall()

2021-08-21 Thread Rondevous


Rondevous  added the comment:

Oops, I was wrong about re.finditer :D
Sorry, I think didn't check that properly.

Just saw the changes. The patch looks good :)

Thanks a lot!

--
title: Suggest the use of non-capturing groups in re.findall() and 
re.finditer() docs -> Clarify the documentation of re.findall()

___
Python tracker 
<https://bugs.python.org/issue44940>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44940] Suggest the use of non-capturing groups in re.findall() and re.finditer() docs

2021-08-20 Thread Rondevous


Rondevous  added the comment:

To produce the same results that you'd get by using the global flag in 
javascript regex, and make re.findall to not capture the groups exclusively, 
all the groups in the pattern need to be of the non-capturing (?:) type. 

If the distinction about capturing and non-capturing groups is mentioned in the 
docs of re.findall, it would help those who have learnt regex from another 
language (like javascript), where the global flag in regex is allowed.

I want the docs of re.findall and re.finditer to somehow suggest the use 
(?:group) to return the original matches and not the captured groups.

--

___
Python tracker 
<https://bugs.python.org/issue44940>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44940] Suggest the use of non-capturing groups in re.findall() and re.finditer() docs

2021-08-20 Thread Rondevous


Rondevous  added the comment:

Maybe the functionality of re.findall and re.finditer is limited because, e.g. 
I can't do something like this:
https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-in-regular-expressions#3513858

The workaround for doing that might need me to eventually write a parser O_O

--

___
Python tracker 
<https://bugs.python.org/issue44940>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44940] Suggest the use of non-capturing groups in re.findall() and re.finditer() docs

2021-08-20 Thread Rondevous


Rondevous  added the comment:

>From my understanding, "|" should match either the RegEx on the left or the 
>RegEx on the right of the pipe
>>> help(re):
"|"  A|B, creates an RE that will match either A or B.

With re.search(), the pattern below matches 'cool' as well as 'foo'
>>> re.search('(foo)|cool?', 'foo bar cool foobar coolbar')

>>> re.search('(foo)|cool?', 'cool')


But, the same pattern and strings won't match 'cool' if used with re.findall() 
or re.finditer() because of how they work when capture-groups are present in 
the pattern.

--
title: Hint the use of non-capturing group in re.findall() documentation -> 
Suggest the use of non-capturing groups in re.findall() and re.finditer() docs

___
Python tracker 
<https://bugs.python.org/issue44940>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44940] Hint the use of non-capturing group in re.findall() documentation

2021-08-20 Thread Rondevous


Rondevous  added the comment:

To clarify in short: the pattern I mentioned doesn't give the result I expected 
in re.findall() unlike re.search()

Given pattern:  (foo)?bar|cool

Maybe my approach in testing the regex first using re.search() and then using 
re.findall() to return all matches was wrong.

Initially, after going through help(re) I had associated re.findall with the 
'global' flag used in javascript regex which would return all the matches. 
Without the global flag (in javascript) only the first match is returned, like 
re.search() in python.

--

___
Python tracker 
<https://bugs.python.org/issue44940>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44940] Hint the use of non-capturing group in re.findall() documentation

2021-08-17 Thread Rondevous


New submission from Rondevous :

Can it please be hinted in the docs of re.findall to use (?:...) for 
non-capturing groups?

>>> re.findall('(foo)?bar|cool', 'cool')
['']
>>>
### I expected the result: ['cool']

After hours of frustration, I learnt that I should use a non-capturing group 
(?:foo) in the pattern. This was not obvious.


P.S. Making the groups non-capturing in such a pattern is not needed in 
javascript (as tested on regexr.com); could this be an issue with the | 
operator in re.findall?

--
assignee: docs@python
components: Documentation
messages: 399799
nosy: docs@python, rondevous
priority: normal
severity: normal
status: open
title: Hint the use of non-capturing group in re.findall() documentation
type: enhancement
versions: Python 3.8

___
Python tracker 
<https://bugs.python.org/issue44940>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42448] re.findall have different match result against re.search or re.sub

2021-08-17 Thread Rondevous


Rondevous  added the comment:

I was frustrated for hours when I couldn't figure out why this won't match:

>>> re.findall(r'(foo)?bar|cool', 'cool')

Now I know, I have to make this change: (?:foo)
But this isn't obvious.
Should it be mentioned in the docs of re.findall() to use (?:...) for 
non-capturing groups?

--
nosy: +rondevous

___
Python tracker 
<https://bugs.python.org/issue42448>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com