Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A Why python regexps look expecting a begin match, but not an ending one?

It seems matching lines starting with "test", but not the ones ending with it (or containining it somewhere). Yes, re.match matches lines that start with the described pattern: If zero or m...

posted 15h ago by Karl Knechtel‭

Answer
#1: Initial revision by user avatar Karl Knechtel‭ · 2025-06-06T04:36:08Z (about 15 hours ago)
> It seems matching lines starting with "test", but not the ones ending with it (or containining it somewhere).

Yes, [re.match](https://docs.python.org/3/library/re.html#re.match) matches lines that start with the described pattern:

> If zero or more characters at the beginning of *string* match the regular expression *pattern*, return a corresponding Match.

To check that the pattern describes the entire line, use [re.fullmatch](https://docs.python.org/3/library/re.html#re.fullmatch); to do a substring match, use [re.search](https://docs.python.org/3/library/re.html#re.search).

There's also a [separate heading in the documentation](https://docs.python.org/3/library/re.html#search-vs-match) describing these differences.

Besides choosing the operation to do with the regex, you can also use "anchors" for the pattern that only match at the beginning or end of the string. To match at the beginning, use `^` as the first character in the regex; to match at the end, use `$` as the last character. These are "zero-width" matches; when the regex engine checks for them, it doesn't associate them with any characters from the input string - it only checks the current position as it's matching.

> Why?

Of course we should have different functions to do different things. The remaining question is why we should have a `match` at all.

The simplest explanation I can think of is that it's easy to implement efficiently, and often useful. In particular, you can easily and efficiently implement `fullmatch` in terms of `match` - first match, and then see whether there is anything left in the input string after matching the pattern. But we can't do it the other way around: if we only have `fullmatch` and want to get the `match` effect, we can only modify the regex pattern to have "also match any characters after that" (`.*`), and matching against that takes extra time (or special work to optimize the regex engine).

Meanwhile, a `search` for a substring must be slower - in the worst case, you basically need to check at every position in the input.

> but not the ones ending with it 

It should be noted that there isn't an efficient way to check whether the input ends with a regex. That's because matching a regex requires scanning forwards in the string from some starting point, but the regex pattern doesn't match a fixed amount of data. Therefore, if the pattern matches at the end, we don't know where to start looking - we need to do the slow searching operation, and then see if one of those matches is at the end.