Question: I have a text file which includes lines like this:

```
<pattern number=1 theme=pseudo>
<pattern number=2 theme=foo>
<pattern number=3 theme=bar>

```

I'm selecting a random line with this function:

```
def find_random_pattern(theme):
    found_lines = []
    pattern = open('poempatterns.txt','r')
    for line in pattern:
        found = re.findall("theme="+theme,line)
        for match in found:
            found_lines.append(line)

    selectedline = random.choice(found_lines)
    return selectedline

```

let's say it returned `<pattern number=1 theme=pseudo>`

When I check it with this condition, it returns `False`

```
if find_random_pattern("pseudo") == "<pattern number=1 theme=pseudo>":
    return True
else:
    return False

```

Why these two strings are not matching?

Answer: 
You are expecting that `re.findall` returns the whole line but it returns the matched part:

```
>>> line = "<pattern number=1 theme=pseudo>"
>>> import re
>>> re.findall("theme=pseudo", line)
['theme=pseudo']

```

I suggest you use a pattern that matches the whole line, for example:

```
>>> re.findall(".*theme=pseudo.*", line)
['<pattern number=1 theme=pseudo>']

```

Your final code would look like:

```
def find_random_pattern(theme):
    found_lines = []
    pattern = open('poempatterns.txt','r')
    for line in pattern:
        # Call strip() just in case you have some blank spaces or \n at the end
        found = re.findall(".*theme=%s.*" % theme, line.strip())
        for match in found:
            found_lines.append(line)

    selectedline = random.choice(found_lines)
    return selectedline

```

A solution a bit more concise would be:

```
import random
import re
import string

def find_random_pattern(theme):
    lines = open('poempatterns.txt','r').readlines()
    stripped_lines = map(string.strip, lines)
    found_lines = filter(lambda l: re.match(".*theme=%s.*" % theme, l), stripped_lines)
    return random.choice(found_lines)

```
`re.findall` does return the match only not the whole `line`. 
Quoting from [Python Docs.](https://docs.python.org/3/library/re.html?highlight=re#module-re) :

> 
> re.findall(pattern, string, flags=0) Return all non-overlapping
>  matches of pattern in string, as a list of strings. The string is
>  scanned left-to-right, and matches are returned in the order found. If
>  one or more groups are present in the pattern, return a list of
>  groups; this will be a list of tuples if the pattern has more than one
>  group. Empty matches are included in the result unless they touch the
>  beginning of another match.
> 
> 
> 

**DEMO:**

```
>>> s = 'Good morning Gotham!'
>>> re.findall('Go', s)
['Go', 'Go']

```

That's being said, if your requirement is to find if `theme` exits in `line`, then you really don't need `regex` for that:

```
def find_random_pattern(theme):
    with open('poempatterns.txt','r') as pattern:
        found_lines = [line for line in pattern if theme in line]
    selectedline = random.choice(found_lines)
    return selectedline

```