LogoReturn to Home RegEx
Integrations
All integrations
AWS API
AWS Lambda
DynamoDB
Oracle
Redshift
Snowflake
GraphQL
Supabase
Twilio
Azure Blob Storage
Slack
SendGrid
Generic HTTP API
AWS S3
Stripe
Microsoft SQL
Salesforce
PostgreSQL
MySQL
MongoDB
HubSpot
Google Sheets
Google BigQuery
Firebase
Airtable
Integrations
About UI Bakery
Log in
Request UI Bakery demo
RegEx library
Email regex PHP
Phone number regex PHP
IP address regex PHP
Date regex PHP
URL regex PHP
Numbers only regex (digits only) PHP
UUID regex PHP
Regex match words PHP
ZIP code regex PHP
GUID regex PHP
Password regex PHP
HTML regex PHP
SSN regex PHP
XML regex PHP
Mac address regex PHP
Street address regex PHP

Regex match words PHP

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.

Discover UI Bakery – an intuitive visual internal tools builder. Try it now!
JavaScript
Python
Java
C#
PHP
No items found.

Simple word match

The regular expression to match only words looks like this (including compound words):

"/^\\b(?:\\w|-)+\\b$/"
Test it!
/^\b(?:\w|-)+\b$/

True

False

Enter a text in the input above to see the result

Example code in PHP:

// Validate word
$words_regex = "/^\\b(?:\\w|-)+\\b$/"; 
echo preg_match($words_regex, 'word'); // returns 1
echo preg_match($words_regex, 'pet-friendly'); // returns 1
echo preg_match($words_regex, 'not a word'); // returns 0

// Extract words from a string
$extract_words_pattern = "/\\b(?:\\w|-)+\\b/";
$string_to_match = 'Hello, world!';
preg_match_all($extract_words_pattern, $string_to_match, $matches);
print_r($matches[0])// matches[0] is ['Hello', 'world']

‍

Test it!

True

False

Enter a text in the input above to see the result

Test it!

True

False

Enter a text in the input above to see the result

Create an internal tool with UI Bakery

Discover UI Bakery – an intuitive visual internal tools builder.

Try it now