c# - 检查文本中的正负字符串(C# 到 Objective-C )

标签 c# objective-c regex

我有一个文本字符串,我想检查它的几个条件。 我在 C# 中有这个函数,我想将它移植到 Objective C (我是新手) 如果检查函数包含 (1) 中的任何一个且不包含 (2) 中的任何一个,则应返回 true

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

keyword1 

或者

keyword2 AND keyword3

或者

keyword4

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

keyword4

或者

keyword5

或者

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;
}

最佳答案

将肯定和否定关键字的列表以及要检查的文本转换为 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]];
}

关于c# - 检查文本中的正负字符串(C# 到 Objective-C ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23653379/

相关文章:

c# - 如何遍历日期范围?

ios - 在 swift 3 中使用自定义 View 选择多个日期范围

java - 提取驱动器号,java正则表达式

mysql - 如何识别混入汉字的全英文单词?

php - 基本正则表达式帮助

c# - 使用 CrystalDecisions.CrystalReports.Engine 从 Crystal Report 获取抑制条件和格式条件

c# - 无法用另一个 .exe 启动 .exe

c# - 如何自定义 text-danger 给出的验证错误信息?

iphone - 在哪个线程中调用 iOS 完成处理程序 block ?

ios - Tweetbot URL Scheme 没有打开用户