Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re.match failed to match left square brackets as the first char #74145

Closed
boqu mannequin opened this issue Apr 1, 2017 · 3 comments
Closed

re.match failed to match left square brackets as the first char #74145

boqu mannequin opened this issue Apr 1, 2017 · 3 comments
Labels
topic-regex type-bug An unexpected behavior, bug, or error

Comments

@boqu
Copy link
Mannequin

boqu mannequin commented Apr 1, 2017

BPO 29959
Nosy @ezio-melotti, @serhiy-storchaka

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-04-01.07:44:16.345>
created_at = <Date 2017-04-01.07:38:49.820>
labels = ['expert-regex', 'type-bug', 'invalid']
title = 're.match failed to match left square brackets as the first char'
updated_at = <Date 2017-04-05.06:19:52.816>
user = 'https://bugs.python.org/boqu'

bugs.python.org fields:

activity = <Date 2017-04-05.06:19:52.816>
actor = 'bo qu'
assignee = 'none'
closed = True
closed_date = <Date 2017-04-01.07:44:16.345>
closer = 'serhiy.storchaka'
components = ['Regular Expressions']
creation = <Date 2017-04-01.07:38:49.820>
creator = 'bo qu'
dependencies = []
files = []
hgrepos = []
issue_num = 29959
keywords = []
message_count = 3.0
messages = ['290968', '290969', '291158']
nosy_count = 4.0
nosy_names = ['ezio.melotti', 'mrabarnett', 'serhiy.storchaka', 'bo qu']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue29959'
versions = ['Python 3.5']

@boqu
Copy link
Mannequin Author

boqu mannequin commented Apr 1, 2017

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]']

@boqu boqu mannequin added topic-regex type-bug An unexpected behavior, bug, or error labels Apr 1, 2017
@serhiy-storchaka
Copy link
Member

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

@boqu
Copy link
Mannequin Author

boqu mannequin commented Apr 5, 2017

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\>


@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-regex type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant