findall only works with a string as input not a list.
You probably want to use map and re.match or re.search for example:
Also your regex has multiple repeat symbols in it and needs some tuning, this one seems to work J\w{3}son
import re
a = ["Jackson", "Johnson", "Jason"]
c = list(map(lambda x: re.search("J\w{3}son",x), a))
print([i.string for i in c if i])
output:
['Jackson', 'Johnson']
Update if your input type is just a string then your original expression was fine you just need to change the regex to the example above
import re
a = "Jackson Johnson Jason"
b = re.findall("J\w{3}son", a)
print(b)
output:
['Jackson', 'Johnson']
Answer from Alexander on Stack OverflowHow to use Python regex pattern matching with re.findall(pattern, string)? - Stack Overflow
Python Regex and re.findall problems
regex - How can I find all matches to a regular expression in Python? - Stack Overflow
how to re.findall
Videos
Use re.findall or re.finditer instead.
re.findall(pattern, string) returns a list of matching strings.
re.finditer(pattern, string) returns an iterator over MatchObject objects.
Example:
re.findall( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats')
# Output: ['cats', 'dogs']
[x.group() for x in re.finditer( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats')]
# Output: ['all cats are', 'all dogs are']
Another method (a bit in keeping with OP's initial spirit albeit 13 years later) is to compile the pattern and call search() on the compiled pattern and move along the pattern. This is a bit verbose but if you don't want a lookahead etc. or you want to search over a string more explicitly, then you can use the following function.
import re
def find_all_matches(pattern, string, group=0):
pat = re.compile(pattern)
pos = 0
out = []
while m := pat.search(string, pos):
pos = m.start() + 1
out.append(m[group])
return out
pat = r'all (.*?) are'
s = 'all cats are smarter than dogs, all dogs are dumber than cats'
find_all_matches(pat, s) # ['all cats are', 'all dogs are']
find_all_matches(pat, s, group=1) # ['cats', 'dogs']
This works for overlapping matches too:
find_all_matches(r'(\w\w)', "hello") # ['he', 'el', 'll', 'lo']
how to use re.findall so that it outputs from code = 'a, b, c' is ['a', 'b', 'c'] because a = re.findall([r'\D+,'], code) outputs ['a, b,']
I'm sorry if the way I phrased that question made absolutely no sense, but here is my code:
>>>> phoneRegex = re.compile(r'((\d\d\d-)?\d\d\d-\d\d\d\d)')
>>>> phoneRegex.findall("Hey, please feel free to call me at 123-555-1234, or if that doesn't work, call me on my cell at 123-123-1234")
[('123-555-1234', '123-'), ('123-123-1234', '123-')]Essentially, I'm confused as to why the result of using the ".findall" method is a tuple. All I want the program to do is to find all instances of a phone number pattern where the area code portion (and the dash after that portion) is optional. The result should be a list of strings with just both phone numbers , no?