检查正文和负字符串的文本(C#到目标C)

时间:2014-05-14 11:27:14

标签: c# objective-c regex

我有一个文本字符串,我想检查它的几个条件。 我在C#中有这个功能,我想将它移植到目标C(我是新手) 如果检查函数包含(1)和(2)

中的任何一个,则它应返回true

(1)包含以下任何内容:

keyword1 

OR

keyword2 AND keyword3

OR

keyword4

(2)不应包含任何关键字

keyword4

OR

keyword5

OR

keyword6

我使用LINQ在C#中完成了这个功能:

  public static string ProcessText(string text, string postiveKeywords, string negativeKeywords)
        {
            //prepare the strings to lists
            var simpleTextKeywords = text.Split(' ');

            //trim any other characters
            for (int i = 0; i < simpleTextKeywords.Length; i++ )

            {
                simpleTextKeywords[i] = simpleTextKeywords[i].Trim(
                    '~','!','@','#','$','%','^','&','*','(',')','_','+','-','=','[',']',';','\'','\\',',','.','/','{','}',':','"','|','<','>','?');
            }

            string [] listOfKeywords;
            if ( !string.IsNullOrWhiteSpace(postiveKeywords))
               listOfKeywords = postiveKeywords.Split(',');
            else
                listOfKeywords = new string[] { };

            string[] listOfnegativeKeywords;
            if ( !string.IsNullOrWhiteSpace(negativeKeywords ))
                listOfnegativeKeywords = negativeKeywords.Split(',');
            else
                listOfnegativeKeywords = new string[] { };


            //parse the keywordlist
            var matches = listOfKeywords
                        .Any(OR => OR.Split('&').All(kw => simpleTextKeywords.Contains(kw)
                            && !listOfnegativeKeywords.Any(x => simpleTextKeywords.Any(y => x == y))
                            && !string.IsNullOrWhiteSpace(kw)));
            if (!matches)
            {
                return string.Empty;
            }

            //return the first match
            return listOfKeywords
                        .First(OR => OR.Split('&')
                        .All(kw => simpleTextKeywords.Contains(kw)
                            && !listOfnegativeKeywords.Any(x => simpleTextKeywords.Any(y => x == y))
                            && !string.IsNullOrWhiteSpace(kw)));
        }

更新 下面是目前的代码:

+ (bool )ProcessText:(NSString*)text
  andPositiveKeyword:(NSString*)positiveKeywords
  andNegativeKeyword:(NSString*)negativeKeywords
{
    //split the text words
    NSMutableArray* simpleTextKeywords = [(NSArray*)[text componentsSeparatedByString:@" "] mutableCopy];

    //trim unwanted characters
    for (int i=0; i< simpleTextKeywords.count; i++) {
        simpleTextKeywords[i] = [simpleTextKeywords[i] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"~!@#$%^&*()_+-=[];\'\\,./{}:\"|<>?"]];
    }

    NSArray* listOfPositiveKeywords = [[NSArray alloc] init];
    if ( ![NSString stringIsNilOrEmpty:positiveKeywords])
    {
        listOfPositiveKeywords = [positiveKeywords componentsSeparatedByString:@","];
    }

    NSArray* listOfNegativeKeywords = [[NSArray alloc] init];
    if ( ![NSString stringIsNilOrEmpty:negativeKeywords])
    {
        listOfNegativeKeywords = [ negativeKeywords componentsSeparatedByString:@","];
    }

    BOOL matches = false;
    for (int i=0; i< listOfPositiveKeywords.count; i++) {
            //if the keyword has "&"
            //all matches should be true
            //split the &
            NSArray* andKeywords = [listOfPositiveKeywords[i] componentsSeparatedByString:@"&"];
             matches = true;
            for (int j=0; j<andKeywords.count; j++) {
                if ( ![simpleTextKeywords  containsObject:andKeywords[j]] )
                {
                    matches = false;
                    break;
                }
            }
        if ( matches == true )
            break;
    }

    //if there are any matches we have to check if it doesnt have any negative keyword
    if ( matches)
    {
        for ( int i =0; i < listOfNegativeKeywords.count;i++)
        {
            if ( [simpleTextKeywords containsObject:listOfNegativeKeywords[i]])
                matches = false;
        }
    }
    return matches;
}

1 个答案:

答案 0 :(得分:2)

将要检查的正面和负面关键字和文字列表转换为NSSet的实例。然后使用NSSet containsObject:方法检查是否包含。

使用NSString方法componentsSeparatedByString:将要搜索的文本拆分为字符串数组。

使用NSString方法stringByTrimmingCharactersInSet:和使用NSCharacterSet方法创建的集合:characterSetWithCharactersInString:来修剪不需要的字符。

修剪的示例代码:

NSString *trimCharacters = @"~!@#$%^&*()_+-=[];\'\\,./{}:\"|<>?";
NSCharacterSet *trimCharacterSet = [NSCharacterSet characterSetWithCharactersInString:trimCharacters];
NSMutableArray *simpleTextKeywords = [NSMutableArray new];
for (NSString *item in textArray) {
    [simpleTextKeywords addObject:[item stringByTrimmingCharactersInSet:trimCharacterSet]];
}