Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Anchors on both end

import re

strings = [
    "123",
    "hello 456 world",
    "hello world",
]

for line in strings:
    if re.search(r'\d+', line):
        print(line)

print('---')

for line in strings:
    if re.search(r'^\d+$', line):
        print(line)


print('---')

for line in strings:
    if re.search(r'\A\d+\Z', line):
        print(line)


Output:

123
hello 456 world
---
123
---
123