问题描述
我只是使用grep命令来执行此操作。
我不确定如何编写正则表达式来查找长度8的所有单词,这些单词在字符串中的任何位置都包含特定字母a
和e
。
\b\w{8}\b
-获取所有8个字符的单词,但如何进一步过滤以删除所有不包含a
或e
的单词。
elephant - good
elafna - bad too short
aassdddd - bad needs to contain e
解决方法
使用
grep -oE '\b[[:alnum:]_]{8}\b' words.txt | grep '[ae]'
\b[[:alnum:]_]{8}\b
模式匹配8个单词的单词,然后第二个grep
仅保留包含a
或e
字母的单词。
Test:
grep -oE '\b[[:alnum:]_]{8}\b' <<< 'sed45678 bash_123 qw3rtyui' | grep '[ae]'
结果:
sed45678
bash_123
,
如果支持grep -P以使用PCRE正则表达式,则可以使用
/*
* The sys-pipe entry.
* Allocate an inode on the root device.
* Allocate 2 file structures.
* Put it all together with flags.
*/
pipe()
{
register *ip,*rf,*wf;
int r;
ip = ialloc(rootdev);
if(ip == NULL)
return;
rf = falloc();
if(rf == NULL) {
iput(ip);
return;
}
r = u.u_ar0[R0];
wf = falloc();
if(wf == NULL) {
rf->f_count = 0;
u.u_ofile[r] = NULL;
iput(ip);
return;
}
u.u_ar0[R1] = u.u_ar0[R0]; /* wf's fd */
u.u_ar0[R0] = r; /* rf's fd */
wf->f_flag = FWRITE|FPIPE;
wf->f_inode = ip;
rf->f_flag = FREAD|FPIPE;
rf->f_inode = ip;
ip->i_count = 2;
ip->i_flag = IACC|IUPD;
ip->i_mode = IALLOC;
}
说明
-
\b(?=\w{8}\b)(?=\w*a)\w*e\w*\b
字边界 -
\b
前瞻性,断言8个字符,其后紧接右边是一个词边界 -
(?=\w{8}\b)
正向前进,在右边声明(?=\w*a)
字符 -
a
匹配一个字符char 0+次,匹配一个\w*e\w*
字符char字符再次0+次 -
e
字边界
例如
\b
输出
grep -oP "\b(?=\w{8}\b)(?=\w*a)\w*e\w*\b" words.txt