我有一个包含短文本字符串的文本文件:
what TIME is it
what TIME is in X if here it is TIME
我想找到并打印“TIME”字样恰好出现两次的所有行。
使用grep可以很容易地找到一个或多个
grep "TIME" file.txt
我也尝试了
grep -E "TIME{2}" file.txt
awk '/TIME{2}/' file.txt
但我只得到空输出。可能是什么问题?怎么解决这个问题?
我从正则表达式中得知的是,当我们使用*时,它表示0或更多匹配,如果我们使用+,则表示一个或多个匹配。最后,如果我们使用{min,max},那么您正在寻找最小到最大匹配。使用{N}告诉引擎准确返回N个匹配。
答案 0 :(得分:2)
以下是你在Perl中如何计算匹配数量的方法。
#!/usr/bin/perl
use strict;
use warnings;
while (<DATA>) {
my @matches = /TIME/g;
print if @matches == 2;
}
__DATA__
what TIME is it
what TIME is in X if here it is TIME
给出这个输出:
what TIME is in X if here it is TIME
答案 1 :(得分:2)
-(void)recurseForumActivity{
@autoreleasepool {
__block __weak dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
myRequestStringForum = [NSString stringWithFormat:@"lastDate=%@&threadTitle=%@&threadCountry=%@&threadCategory=%@&threadSubCategory=%@&getData=0",lastDateForumActivity,searchThreadTitle, searchThreadCountry, searchThreadCategory, searchThreadSubCategory];
responseForum = [self setupPhpCall:myRequestStringForum :@"xxx.php"];
[NSThread sleepForTimeInterval:2.0f];
dispatch_async(dispatch_get_main_queue(), ^{
if(responseForum.length > 0 && ![responseForum isEqualToString:@"[]"]){
labelNewForumThreads.text = [NSString stringWithFormat:@"%@ new threads...", responseForum];
if(imageviewForumAlert == NULL){
UIImage *image = [UIImage imageNamed:@"alert.png"];
imageviewForumAlert = [UIImageView new];
[viewNav1 addSubview:imageviewForumAlert];
imageviewForumAlert.translatesAutoresizingMaskIntoConstraints = NO;
imageviewForumAlert.image = image;
NSDictionary *viewsDictionary = @{@"imageviewForumAlert":imageviewForumAlert};
NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-19-[imageviewForumAlert(12)]-19-|"
options:0
metrics:nil
views:viewsDictionary];
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-19-[imageviewForumAlert(12)]-19-|"
options:0
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraint_H];
[self.view addConstraints:constraint_V];
}else{
imageviewForumAlert.hidden = NO;
}
/**NSDictionary *dic = [response JSONValue];
if((NSNull*)dic != [NSNull null]){
labelNewForumThreads.text = [NSString stringWithFormat:@"%d new threads...", dic.count];
}**/
}else{
imageviewForumAlert.hidden = YES;
labelNewForumThreads.text = [NSString stringWithFormat:@"%d new threads...", 0];
}
/**else{
labelNewForumThreads.text = [NSString stringWithFormat:@"%d new threads...", 0];
}**/
myRequestStringForum = @"";
responseForum = @"";
concurrentQueue = nil;
[self recurseForumActivity];
});
});
}
}
答案 2 :(得分:1)
如果你谈论量词,他们量化的模式必须是连续的。要回答你的问题,你最好使用类似.*?time.*?time.*
的东西,不区分大小写。
https://regex101.com/r/wD4oT8/3
这意味着time
将出现两次,并且它可能包含任何时间任何一方的信息。请注意,这意味着3个或更多提及仍然有效。
我们可以使用相同的东西,但在开头写一个先行来断言&#34;时间&#34;没有提到两次以上。 (?!.*time.*time.*time)^.*?time.*?time.*
答案 3 :(得分:0)
此awk
应该有效:
awk -F 'TIME' 'NF==3' file
what TIME is in X if here it is TIME