This regular expression can be used to validate that a given string contains only characters in it or extract two words from a given string.
The regular expression to match only words looks like this (including compound words):
Enter a text in the input above to see the result
Example code in Python:
# Validate words
words_pattern = "^\\b(?:\\w|-)+\\b$"
re.match(words_pattern, 'word') # Returns Match object
re.match(words_pattern, 'pet-friendly') # Returns Match object
re.match(words_pattern, 'not a word') # Returns None
# Extract words from a string
words_extract_pattern = "\\b(?:\\w|-)+\\b"
re.findall(words_extract_pattern, 'Hello, world!') # returns ['Hello', 'world']
Discover UI Bakery – an intuitive visual internal tools builder.