How to find words surrounded by asteriscs in a text with python and regex

To find a word between asterisks * in a text with Python, you can use regular expressions (regex).

In particular, you can use Python’s re module to compile a regex that looks for the word between the asterisks. The regex r"\*([a-zA-Z0-9_]+)\*" looks for a word between two asterisks, which can contain letters, numbers, and underscores.

Here’s an example Python code that uses the regex to find a word between asterisks in a text:

import re

text = "This is an *example* of text with a word between asterisks *this_word*."

match = re.search(r"\*([a-zA-Z0-9_]+)\*", text)

if match:
    word = match.group(1)
    print(f"The word between the asterisks is: {word}")
else:
    print("There is no word between asterisks in the text.")

This code searches for the first occurrence of a word between asterisks in the string text using the regex r"\*([a-zA-Z0-9_]+)\*". If it finds a match, it prints the word between the asterisks. Otherwise, it prints a message indicating that there is no word between asterisks in the text.

If there are multiple occurrences of words between asterisks in the text, you can use the re.findall function to find all occurrences:

import re

text = "This is an *example* of text with *two* words between asterisks *this_word* and *another_word*."

matches = re.findall(r"\*([a-zA-Z0-9_]+)\*", text)

if matches:
    print("The words between the asterisks are:")
    for match in matches:
        print(match)
else:
    print("There are no words between asterisks in the text.")

The pattern for the search is every word or number with also underscore between two asteriscs:

a-z is every letter

A-Z every letter in capital

0-9 is every number

_ is underscore

+ means that there can be more letter, numbers and underscores

\*( and )\* means… between asteriscs

This code uses the re.findall function to find all occurrences of words between asterisks in the string text. If it finds at least one match, it prints all the words between the asterisks that were found. Otherwise, it prints a message indicating that there are no words between asterisks in the text.

What if we want spaces between more words, instead of underscores only?

import re

text = "This is an *example* of text with *two* words between asterisks *this word with spaces* and *another_word*."

matches = re.findall(r"\*([a-zA-Z0-9_ ]+)\*", text)

if matches:
    print("The words between the asterisks are:")
    for match in matches:
        print(match)
else:
    print("There are no words between asterisks in the text.")
We just add a space after the underscore, if you observe well

Let’s do another example for our italian friends:

import re

text = "Questo è un *esempio* di testo con *due* parole tra asterischi *questa_parola* e *un_altra_parola*."

matches = re.findall(r"\*([a-zA-Z0-9_]+)\*", text)

if matches:
    print("Le parole tra gli asterischi sono:")
    for match in matches:
        print(match)
else:
    print("Non ci sono parole tra gli asterischi nel testo.")

The output will be this:

Le parole tra gli asterischi sono:
esempio
due
questa_parola
un_altra_parola


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.