zoukankan      html  css  js  c++  java
  • 正则表达式的使用 选取有用数据

     //话题  ##

        NSMutableAttributedString * contentStr = [[NSMutableAttributedString alloc] initWithString:Content];

        NSError *error = NULL;

        NSRegularExpression *topicRegex = [NSRegularExpression regularExpressionWithPattern:@"#[^#]+#" options:NSRegularExpressionCaseInsensitive error:&error];

    //找到第一个符合条件的数据

        NSTextCheckingResult *result = [topicRegex firstMatchInString:bm.Content options:0 range:NSMakeRange(0, [bm.Content length])];

        if(result)

        {

            [contentStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:result.range];

            [contentStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]  range:result.range];

            [contentStr endEditing];

        }

         //链接

        NSRegularExpression *linkRegex = [NSRegularExpression regularExpressionWithPattern:@"链接的正则表达式" options:NSRegularExpressionCaseInsensitive error:&error];

    //找到多个符合条件的数据

        NSArray *linkArr = [linkRegex matchesInString:bm.Content options:0 range:NSMakeRange(0, [bm.Content length])];

        NSMutableAttributedString * contentStr = [[NSMutableAttributedString alloc] initWithString:Content];

        if ([linkArr count] != 0) {

            for (NSTextCheckingResult *linkResult in linkArr) {

                NSLog(@"%@ ", [Content substringWithRange:result.range]);

            }

        }

  • 相关阅读:
    围棋GO
    Android 四大组件------Activity
    android studio----设备兼容
    android UI开发--------Action Bar
    开发调试工具----DDMS
    RxJava的使用
    Atlas框架
    Android studio上git的使用
    AS上git的使用与遇到的问题
    picasso-安卓的图片下载缓存库
  • 原文地址:https://www.cnblogs.com/qiutangfengmian/p/4962226.html
Copyright © 2011-2022 走看看