Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all words with 3 letters with regex

I'm trying to find all words with 3 letters in a string.
So in this list

cat monkey dog mouse

I only want

cat dog

This is my expression:

^[a-zA-Z]{3}$

I tested it with different online regex tester, but none of them matched my expression.

like image 704
Evgenij Reznik Avatar asked Oct 08 '15 13:10

Evgenij Reznik


People also ask

How do you search for a word in regex?

To run a “whole words only” search using a regular expression, simply place the word between two word boundaries, as we did with ‹ \bcat\b ›. The first ‹ \b › requires the ‹ c › to occur at the very start of the string, or after a nonword character.

How do I match a pattern in regex?

2.1 Matching a Single Character The fundamental building blocks of a regex are patterns that match a single character. Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" .

Can you Google with regex?

Google Search Console has introduced the use of regular expression to filter results in the performance report. Using the custom filter option at the top of the performance report, you can apply a filter with regex using the Custom (regex) dropdown option.


1 Answers

You should use your match with word boundaries instead of anchors:

\b[a-zA-Z]{3}\b

RegEx Demo

When you use:

^[a-zA-Z]{3}$

It means you want to match a line with exact 3 letters.

like image 176
anubhava Avatar answered Nov 14 '22 23:11

anubhava