#๐Ÿ”’ Regex on readline?

30 messages ยท Page 1 of 1 (latest)

lunar pawnBOT
#

@dreamy mulch

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wet nova
#

that doesn't really has anything to do with readline nor files at all

#

you can use match.string or match.group(0) instead of str(match)

bleak holly
#

is your regex correct?

languid shuttle
#

It returns none if it can't find it

#

!d re.search will return None if there's no match

lunar pawnBOT
#

re.search(pattern, string, flags=0)```
Scan through *string* looking for the first location where the regular expression *pattern* produces a match, and return a corresponding [`Match`](https://docs.python.org/3/library/re.html#re.Match). Return `None` if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.
languid shuttle
#

So you definitely need to check the value of the results to make sure that it is not None before appending

#

findall is different

#

!d re.findall

lunar pawnBOT
#

re.findall(pattern, string, flags=0)```
Return all non-overlapping matches of *pattern* in *string*, as a list of strings or tuples. The *string* is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result.

The result depends on the number of capturing groups in the pattern. If there are no groups, return a list of strings matching the whole pattern. If there is exactly one group, return a list of strings matching that group. If multiple groups are present, return a list of tuples of strings matching the groups. Non-capturing groups do not affect the form of the result.
languid shuttle
#

findall will return a list of matched strings

#

if there's nothing matched, you get an empty list

#

!e

import re
s = "Hello world"
pattern = r'world'
result = re.findall(pattern, s)
print(result)
s = 'bye bye'
result = re.findall(pattern, s)
print(result)
lunar pawnBOT
languid shuttle
#

!e

import re
s = "Hello world"
pattern = r'world'
result = re.search(pattern, s)
print(result)
s = 'bye bye'
result = re.search(pattern, s)
print(result)
lunar pawnBOT
languid shuttle
#

Take your pick I guess

#

If garbage means None, yes

#

idk what that means but OK

#

oh that. So it retuns a Match object and you're supposed to get what you need from it

#

Like result[0] to get the first matching group

#

!e

import re
s = "Hello world"
pattern = r'world'
result = re.search(pattern, s)
if result is not None:
    print(result)
    print(result.group())
    print(result[0])
lunar pawnBOT
languid shuttle
#

Its an object

#

It has its own methods

#

Especially the span so you know at which position the string matched started and ended

#

But you do you

lunar pawnBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.