'Using Python Regex how can I specify beginning and/or end word and only any digits in the middle?

I need to match 'words' (string of characters with no spaces) that might have the word near at the beginning and/or the end and have only digits in the middle.
Examples: near3 4near near2near
It should not match words like nearing3 4nearsighted near3ness nearsighted

I tried this: x = re.match(r"((\bnear)|(near\b))(\d)", txt) It works for this word: near3 and this word: near4near but not for this word 2near



Solution 1:[1]

You can match optional near followed by digits and near OR match near and digits using an alternation using the pipe |

You can surround the alternation with a non capture group and add word boundaries \b at both sides of the pattern to prevent a partial word match.

If you want to match a single digit, you can use only \d instead.

\b(?:(?:near)?\d+near|near\d+)\b

Regex demo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 The fourth bird