美文网首页
正则表达式

正则表达式

作者: Willism | 来源:发表于2017-07-15 09:36 被阅读0次

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self matches %@",@"0\\d\\d-\\d\\d\\d\\d\\d\\d\\d\\d"];

//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self matches %@",@"\\bhi\\b.*\\bLucy\\b"];

//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self matches %@",@"\\d+"];

//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self matches %@",@"\\b\\w{6}\\b"];

//5位到12位的数字

//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self matches %@",@"^\\w{5,12}$"];

//匹配[]中任意一个字符

//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self matches %@",@"^[dfGhjk]$"];

//全角半角字符均匹配

//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self matches %@",@"^[。?!]$"];

//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self matches %@",@"^[0-8]$"];

//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self matches %@",@"^[a-z0-9A-Z_]$"];

//999.999.999.999

//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self matches %@",@"(\\d{1,3}\\.){3}\\d{1,3}"];

//添加公式 判断  1-1000  定规则

NSPredicate *pre = [NSPredicate predicateWithFormat:@"self matches %@",@"((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"];

//((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.)

//

NSString *test = @"2491.255.1.193";

//

BOOL b =  [pre evaluateWithObject:test];

if (b) {

NSLog(@"与公式匹配");

}else{

NSLog(@"不匹配");

}

}

/**

除此之外,下面是cheat sheet的缩小版,和一些简短的解释:

.匹配任一字符。p.p匹配pop,pup,pmp,p@p等等。

\w匹配任意“word-like”字符,包括数字,字母,下划线,不过不能匹配标点符号和其他字符。hello\w会匹配”hello_“,”hello9”和”helloo”,但不匹配”hello!”。

\d 匹配数字,大部分情况下是[0-9]。\d\d?:\d\d会匹配时间格式的字符串,比如”9:30“和”12:45“。

\b 匹配额外的字符,例如空格,标点符号。to\b 会匹配”to the moon”和“to!”中得“to”,但是不会匹配“tomorrow”。\b 用在整个单词的匹配方面和方便。

\s 会匹配空白字符,比如,空格,制表符,换行符。hello\s 会匹配“Well,hello there!”中的 “hello ”。

^用在一行的开始。记住,这个特殊的^不同于方括号中的^!例如,^Hello 会匹配字符串“Hello there”,而不会去匹配“He said Hello”。

$ 用在一行的结束,例如,the end$ 会匹配“It was the end” 而不会去匹配 “the end was near”。

* 匹配 它之前的元素0次或多次。12*3  会匹配 13, 123, 1223, 122223, 和 1222222223。

+ 匹配 它之前的元素1次或多次. 12+3  会匹配  123, 1223, 122223, 和 1222222223。

花括号{}包含了匹配的最大和值最小个数。例如,10{1,2}1会匹配“101”和“1001”,而不会匹配“10001”,因为匹配的最小个数为1,最大个数为2。He[LI]{2,}o会匹配“HeLLo”和“HellLLLIo”和任意其他的“hello”添加多个L的变种,所以没有限制,因为,最少的个数是2,最大的个数没有设置。

有了这些基础知识,就可以继续向下学习了。

*/

@end

相关文章

  • Linux命令行与Shell脚本编程大全-shell正则表达式

    本章内容: 定义正则表达式 了解基本正则表达式 扩展正则表达式 创建正则表达式 定义正则表达式 正则表达式是你定义...

  • 正则相关

    正则表达式基本语法 正则表达式常见字符 正则表达式特殊字符 正则表达式数量词 正则表达式边界匹配 正则表达式逻辑或...

  • 正则表达式系列-1

    正则表达式系列-1正则表达式系列-2正则表达式系列-3正则表达式系列-4 什么是正则表达式 正则表达式就是用事先定...

  • 正则表达式

    正则表达式 - 教程正则表达式 - 简介正则表达式 - 语法正则表达式 - 元字符正则表达式 - 运算符优先级正则...

  • Python基础入门 - 正则表达式与综合实战

    1. 初识正则表达式 1.1 介绍 步骤介绍正则表达式入门及应用正则表达式的进阶正则表达式案例 1.2 正则表达式...

  • Java正则表达式参考

    Java正则表达式入门 java正则表达式应用 深入浅出之正则表达式(一) 深入浅出之正则表达式(二) 正则表达式...

  • 正则表达式

    正则表达式 正则表达式就是记录文本规则的代码 正则表达式常用的元字符 正则表达式常用的限定符 正则表达式举例:这里...

  • Python爬虫(十)_正则表达式

    本篇将介绍python正则表达式,更多内容请参考:【python正则表达式】 什么是正则表达式 正则表达式,又称规...

  • python正则表达式

    本篇将介绍python正则表达式,更多内容请参考:【python正则表达式】 什么是正则表达式 正则表达式,又称规...

  • 正则表达式

    了解正则表达式基本语法 能够使用JavaScript的正则对象 正则表达式简介 什么是正则表达式 正则表达式:用于...

网友评论

      本文标题:正则表达式

      本文链接:https://www.haomeiwen.com/subject/yveahxtx.html