Question: Regex program to print words from sentences which contain 2 numbers and they are not next to each other. I made a regex program which prints them but in some cases they print out words with numbers next to each other.

```
import re
tekst = str(input("Unesi tekst: "))
pattern = re.findall(r'\b(?=\d*[^\W\d])[^\W\d]*\d[^\W\d]*\d\w*\b',tekst)
print(pattern)

```

input:

```
2test2
te22st
2tes2t
t2est2

```

output:

```
2test2
2tes2t
t2est2

```

Answer: 
Use `re.findall`:

```
[i for i in tekst.split() if len(re.findall("\d+", i)) == 2]

```

Output:

```
['2test2', '2tes2t', 't2est2']

```
You could update the pattern to repeat the middle negated character class 1+ times to prevent the 2 digits being next to each other.

```
\b(?=\d*[^\W\d])[^\W\d]*\d[^\W\d]+\d[^\W\d]*\b

```

**Explanation**

* `\b` Word boundary
* `(?=\d*[^\W\d])` Assert as least a single word char that is not a digit
* `[^\W\d]*` Match 0+ times a word char without a digit
* `\d` Match the first digit
* `[^\W\d]1` Match 1+ times a word char without a digit to prevent them begin next to each other
* `\d` Match the second digit
* `[^\W\d]*` Match 0+ times a word char without a digit
* `\b` Word boundary

[Regex demo](https://regex101.com/r/hGEpIj/1)