How to check if a string includes $sometext$ without numbers in Python

1 Answer

0 votes
import re

def include_dollar_symbol_text(text: str) -> bool:
    # Regex to match $word$ (case-insensitive)
    cleaned_text = re.sub(r"\$[a-z]+\$", "", text, flags=re.IGNORECASE)

    # Check if any $ remains
    return "$" not in cleaned_text


print(include_dollar_symbol_text("abc xy $text$ z"))            # ok
print(include_dollar_symbol_text("abc xy $ text$ z"))           # space
print(include_dollar_symbol_text("abc xy $$ z"))                # empty
print(include_dollar_symbol_text("abc 100 $text$ z"))           # ok
print(include_dollar_symbol_text("abc $1000 $text$ z"))         # open $
print(include_dollar_symbol_text("abc xy $IBM$ z $Microsoft$")) # ok
print(include_dollar_symbol_text("abc xy $F3$ z"))              # include number
print(include_dollar_symbol_text("abc xy $text z"))             # missing close $



'''
run:

True
False
False
True
False
True
False
False

'''

 



answered Jul 11, 2025 by avibootz
...