Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 用正则表达式解析html字符串_Iphone_Ios_Objective C_Regex - Fatal编程技术网

Iphone 用正则表达式解析html字符串

Iphone 用正则表达式解析html字符串,iphone,ios,objective-c,regex,Iphone,Ios,Objective C,Regex,我有NSString和这样的成对字符串,465544664646在它们之间变化: data-context-item-title="465544664646" 如何将465544664646字符串解析为数组 编辑 试试这个: NSString *yourString=@"data-context-item-title=\"465544664646\" data-context-item-title=\"1212121212\""; NSMutableArray *substrings = [

我有
NSString
和这样的成对字符串,
465544664646
在它们之间变化:

data-context-item-title="465544664646"
如何将
465544664646
字符串解析为数组

编辑

试试这个:

NSString *yourString=@"data-context-item-title=\"465544664646\" data-context-item-title=\"1212121212\"";

NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:yourString];
[scanner scanUpToString:@"\"" intoString:nil];
while(![scanner isAtEnd]) {
    NSString *tempString;
    [scanner scanString:@"\"" intoString:nil];
    if([scanner scanUpToString:@" " intoString:&tempString]) {
        [substrings addObject:tempString];
    }
    [scanner scanUpToString:@"\"" intoString:nil];
}

//NSLog(@"->%@",substrings); //substrings contains all numbers as string.


for (NSString *str in substrings) {
    NSLog(@"->%ld",[str integerValue]); //converted each number to integer value, if you want to store as NSNumber now you can store each of them in array
}
像这样的

    -(NSString *)stringFromOriginalString:(NSString *)origin betweenStartString:    (NSString*)start andEndString:(NSString*)end {
    NSRange startRange = [origin rangeOfString:start];
    if (startRange.location != NSNotFound) {
        NSRange targetRange;
        targetRange.location = startRange.location + startRange.length;
        targetRange.length = [origin length] - targetRange.location;
        NSRange endRange = [origin rangeOfString:end options:0 range:targetRange];
        if (endRange.location != NSNotFound) {
            targetRange.length = endRange.location - targetRange.location;
            return [origin substringWithRange:targetRange];
        }
    }
    return nil;
}
你可以用

- (NSArray *)componentsSeparatedByString:(NSString *)separator
方法式

NSArray *components = [@"data-context-item-title="465544664646" componentsSeparatedByString:@"\""];
现在你应该在2点拿到绳子。索引

[components objectAtIndex:1]
现在可以使用此处的方法从该字符串创建数组

我找到了一些方法,但我只成功了一个字符串,而不是所有字符串。您的字符串是什么?是不是
data context item title=“465544664646”
您想提取
465544664646
并将其存储在数组中?@MTA,即使代码不起作用,也请显示它!我有一个包含大量
数据上下文项title=“*******”
的大字符串,我想获得所有的****(可以是任何内容)请提供有关字符串的更多信息。你的字符串是否可能包含“其他地方”?
[components objectAtIndex:1]