#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位的數(shù)字
//? ? NSPredicate *pre = [NSPredicate predicateWithFormat:@"self matches %@",@"^\\w{5,12}$"];
//匹配[]中任意一個(gè)字符
//? ? 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? 定規(guī)則
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的縮小版,和一些簡(jiǎn)短的解釋:
.匹配任一字符薪者。p.p匹配pop,pup,pmp,p@p等等斯棒。
\w匹配任意“word-like”字符,包括數(shù)字劳坑,字母毕谴,下劃線,不過不能匹配標(biāo)點(diǎn)符號(hào)和其他字符距芬。hello\w會(huì)匹配”hello_“,”hello9”和”helloo”,但不匹配”hello!”涝开。
\d 匹配數(shù)字,大部分情況下是[0-9]框仔。\d\d?:\d\d會(huì)匹配時(shí)間格式的字符串舀武,比如”9:30“和”12:45“。
\b 匹配額外的字符离斩,例如空格银舱,標(biāo)點(diǎn)符號(hào)。to\b 會(huì)匹配”to the moon”和“to!”中得“to”,但是不會(huì)匹配“tomorrow”跛梗。\b 用在整個(gè)單詞的匹配方面和方便寻馏。
\s 會(huì)匹配空白字符,比如核偿,空格诚欠,制表符,換行符宪祥。hello\s 會(huì)匹配“Well,hello there!”中的 “hello ”聂薪。
^用在一行的開始。記住蝗羊,這個(gè)特殊的^不同于方括號(hào)中的^!例如藏澳,^Hello 會(huì)匹配字符串“Hello there”,而不會(huì)去匹配“He said Hello”耀找。
$ 用在一行的結(jié)束翔悠,例如,the end$ 會(huì)匹配“It was the end” 而不會(huì)去匹配 “the end was near”野芒。
* 匹配 它之前的元素0次或多次蓄愁。12*3? 會(huì)匹配 13, 123, 1223, 122223, 和 1222222223。
+ 匹配 它之前的元素1次或多次. 12+3? 會(huì)匹配? 123, 1223, 122223, 和 1222222223狞悲。
花括號(hào){}包含了匹配的最大和值最小個(gè)數(shù)撮抓。例如,10{1摇锋,2}1會(huì)匹配“101”和“1001”丹拯,而不會(huì)匹配“10001”站超,因?yàn)槠ヅ涞淖钚€(gè)數(shù)為1,最大個(gè)數(shù)為2乖酬。He[LI]{2,}o會(huì)匹配“HeLLo”和“HellLLLIo”和任意其他的“hello”添加多個(gè)L的變種死相,所以沒有限制,因?yàn)橐瘢钌俚膫€(gè)數(shù)是2算撮,最大的個(gè)數(shù)沒有設(shè)置。
有了這些基礎(chǔ)知識(shí)县昂,就可以繼續(xù)向下學(xué)習(xí)了肮柜。
*/
@end