使用正则表达式查找所有3个字母的单词 [英] Find all words with 3 letters with regex

查看:572
本文介绍了使用正则表达式查找所有3个字母的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找字符串中包含3个字母的所有单词.
所以在这个列表中

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

cat monkey dog mouse

我只想要

cat dog

这是我的表情:

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

我用其他在线正则表达式测试器对其进行了测试,但是没有一个匹配我的表情.

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

推荐答案

您应该将匹配项与单词边界一起使用,而不要使用锚点:

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

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

RegEx演示

使用时:

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

这意味着您想将一行精确匹配3个字母.

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

这篇关于使用正则表达式查找所有3个字母的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆