Bash中的正则表达式(分组)

时间:2015-06-08 06:53:17

标签: regex bash regex-group

所以这就是我所拥有的:

regex="^([A-Z]{2,8}-[0-9]{1,4}[[:space:]])+[[:alnum:]]+$"
message="TEST-121 Testing" 
echo $message | grep -qE "$regex"

这很有效。但是,我需要以下内容:

message="TEST-132 Testing is going on". 

我当前的正则表达式,返回1,这应该返回0.还有以下一个:

message+"TEST-111 TEST-132 Stack overflow rocks "

上面的正则表达式只在第一个字符串之后只有一个单词,所以需要帮助将[[:alnum:]]更新为可以解析更多单词而不只是一个单词的东西。

提前致谢。

2 个答案:

答案 0 :(得分:1)

#include<time.h> void game() { int s = 0 , m = 0; char a[]="Computers"; char b[10]; clock_t start, end; double time_used; // timer(s,m,true); comment out this line start = clock(); while (strcmpi(a,b)!=0) { cout<<"Guess the word:"; gets(b); } end = clock(); time_used = ((double) (end - start)) / CLOCKS_PER_SEC; timer(s,m,false); cout<<"You got it correct!\n"; cout<<"Time taken : "<<time_used<<" seconds"<<endl; 添加到最后一个字符类

- (IBAction)playMovie:(UIButton *)sender {

NSURL *url = [[NSURL alloc] initWithString:urlStr.text];
self.movie = [[MPMoviePlayerController alloc]initWithContentURL:url];
self.movie.controlStyle = MPMovieControlStyleNone;

self.movie.shouldAutoplay = YES;
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];

playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
[playButton setImage:[UIImage imageNamed:@"apple.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(playButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
}

答案 1 :(得分:0)

这个正则表达式应该对所有输入字符串起作用:

regex='^[A-Z]{2,8}-[0-9]{1,4}([[:blank:]]+[^[:blank:]]*)+$'

示例:

echo "TEST-111 TEST-132 Stack overflow rocks " | 
    grep -oE '^[A-Z]{2,8}-[0-9]{1,4}([[:blank:]]+[^[:blank:]]*)+$'
TEST-111 TEST-132 Stack overflow rocks