Grep正则表达式,用于可变长度字符串中的数字 [英] Grep regular expression for digits in character string of variable length

查看:336
本文介绍了Grep正则表达式,用于可变长度字符串中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要某种方法来查找包含字符和数字的任意组合,但仅4位数字和至少一个字符的单词.

I need some way to find words that contain any combination of characters and digits but exactly 4 digits only, and at least one character.

示例:

a1a1a1a1        // Match
1234            // NO match (no characters)
a1a1a1a1a1      // NO match
ab2b2           // NO match
cd12            // NO match
z9989           // Match
1ab26a9         // Match
1ab1c1          // NO match
12345           // NO match
24              // NO match
a2b2c2d2        // Match
ab11cd22dd33    // NO match

推荐答案

我不确定您可能要接受的所有其他输入(即ax12ax12ax12ax12是否有效?),但这将根据您发布的内容起作用:

I'm not sure about all the other input you might take (i.e. is ax12ax12ax12ax12 valid?), but this will work based on what you posted:

%> grep -P "^(?:\w\d){4}$" fileWithInput

这篇关于Grep正则表达式,用于可变长度字符串中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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