This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: re.match failed to match left square brackets as the first char
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: bo qu, ezio.melotti, mrabarnett, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-04-01 07:38 by bo qu, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg290968 - (view) Author: bo qu (bo qu) Date: 2017-04-01 07:38
if "[" is the first char in a string, then re.match can't match any pattern from the string, but re.findall works fine

details as follows:

[da@namenode log]$ python3
Python 3.4.3 (default, Jun 14 2015, 14:23:40) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> cyzd="[abc]"
>>> cyzd
'[abc]'
>>> pattern="ab(.*)"
>>> pattern
'ab(.*)'
>>> match=re.match(pattern, cyzd)
>>> match
>>> pattern=r'ab(.*)'     
>>> re.findall(pattern, cyzd)
['c]']
msg290969 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-04-01 07:44
re.match() checks if the beginning of the string matches the regular expression pattern. Use re.search() if you want to find the match not at the beginning of the string.

https://docs.python.org/3/library/re.html#re.match
https://docs.python.org/3/library/re.html#re.search
msg291158 - (view) Author: bo qu (bo qu) Date: 2017-04-05 06:19
hi Serhiy Storchaka
thank you for your explanation, it helps a lot

2017-04-01 15:44 GMT+08:00 Serhiy Storchaka <report@bugs.python.org>:

>
> Serhiy Storchaka added the comment:
>
> re.match() checks if the beginning of the string matches the regular
> expression pattern. Use re.search() if you want to find the match not at
> the beginning of the string.
>
> https://docs.python.org/3/library/re.html#re.match
> https://docs.python.org/3/library/re.html#re.search
>
> ----------
> nosy: +serhiy.storchaka
> resolution:  -> not a bug
> stage:  -> resolved
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue29959>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:58:44adminsetgithub: 74145
2017-04-05 06:19:52bo qusetmessages: + msg291158
2017-04-01 07:44:16serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg290969

resolution: not a bug
stage: resolved
2017-04-01 07:38:49bo qucreate