DEV Community

Kozo Oeda
Kozo Oeda

Posted on • Edited on

Implementing auto-completion by Regex

By using an input as a pattern (which is the reverse of the typical use case), you can easily implement a simple word auto-completion feature!

import re

example_words = ["Python", "Linux", "HHKB", "Pizza", "Ninja"]

pattern = input()

for word in example_words:
    if re.match(pattern, word, re.IGNORECASE):
        print(word)
Enter fullscreen mode Exit fullscreen mode

Input example

li
Enter fullscreen mode Exit fullscreen mode

Output

Linux
Enter fullscreen mode Exit fullscreen mode

Version: Python 3

Top comments (0)