NSString+BBARegex.h
3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// NSString+BBARegex.h
// BBAFoundation
//
// Created by Zhu,Yusong on 2018/9/20.
// Copyright © 2018年 Baidu. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "BDPFoundationDefines.h"
/**
* 字符串正则相关的扩展
* 详细介绍请看README-BBAString
*/
// 生成正则表达式,如果正则表达式错误会触发断言
#define BBARegex(regexString) [NSString bdp_regularExpressionWithRegexString:regexString]
typedef NS_OPTIONS(NSUInteger, BBARegexOptions){
BBARegexOptions_Exclude = 1 << 0, //!< 排除选项(即查找不在方括号的字符) ^
BBARegexOptions_Number = 1 << 1, //!< 数字选项 0-9
BBARegexOptions_Letter = 1 << 2, //!< 字母选项 a-zA-Z
BBARegexOptions_Chinese = 1 << 3, //!< 汉字选项 \\u4e00-\\u9fa5
} __deprecated_enum_msg("please use BDPRegexOptions");
typedef NS_OPTIONS(NSUInteger, BDPRegexOptions){
BDPRegexOptionsExclude = 1 << 0, //!< 排除选项(即查找不在方括号的字符) ^
BDPRegexOptionsNumber = 1 << 1, //!< 数字选项 0-9
BDPRegexOptionsLetter = 1 << 2, //!< 字母选项 a-zA-Z
BDPRegexOptionsChinese = 1 << 3, //!< 汉字选项 \\u4e00-\\u9fa5
};
@interface NSString (BBARegex)
/**
* @brief 初始化并返回 NSRegularExpression 建议使用 BBARegularExpression
*
* @param regexString 正则语句
* @return NSRegularExpression 注意:如果正则规则不合法,在debug模式下触发断言,否则返回nil
*/
+ (NSRegularExpression *)bba_regularExpressionWithRegexString:(NSString *)regexString BDP_FOUNDATION_DEPRECATED("please use bdp_regularExpressionWithRegexString:");
+ (NSRegularExpression *)bdp_regularExpressionWithRegexString:(NSString *)regexString;
/**
* @brief 是否完全匹配正则表达式
*
* @param regex 正则表达式
* @return BOOL
*/
- (BOOL)bba_isMatchedWithRegex:(NSRegularExpression *)regex BDP_FOUNDATION_DEPRECATED("please use bdp_isMatchedWithRegex:");
- (BOOL)bdp_isMatchedWithRegex:(NSRegularExpression *)regex;
/**
* @brief 匹配并返回第一条匹配正则表达式的子串
*
* @param regex 正则表达式
* @return NSString
*/
- (NSString *)bba_findFirstStringWithRegex:(NSRegularExpression *)regex BDP_FOUNDATION_DEPRECATED("please use bdp_findFirstWithRegex:");
- (NSString *)bdp_findFirstWithRegex:(NSRegularExpression *)regex;
/**
* @brief 匹配并返回所有匹配正则表达式的子串的数组
*
* @param regex 正则表达式
* @return NSArray
*/
- (NSArray<NSString *> *)bba_findAllStringsWithRegex:(NSRegularExpression *)regex BDP_FOUNDATION_DEPRECATED("please use bdp_findAllWithRegex:");
- (NSArray<NSString *> *)bdp_findAllWithRegex:(NSRegularExpression *)regex;
/**
* @brief 剔除所有匹配正则表达式的子串
*
* @param regex 正则表达式
* @return NSString
*/
- (NSString *)bba_trimmingWithRegex:(NSRegularExpression *)regex BDP_FOUNDATION_DEPRECATED("please use bdp_trimWithRegex:");
- (NSString *)bdp_trimWithRegex:(NSRegularExpression *)regex;
#pragma mark *** GenerateRegex ***
/// 生成正则表达式
/**
* @brief 针对指定校验正则选项,生成正则表达式
*
* @param options 校正的类型
* @return BOOL
*/
+ (NSRegularExpression *)bba_generateRegexWithRegexOptions:(BBARegexOptions)options BDP_FOUNDATION_DEPRECATED("please use bdp_generateRegexWithRegexOptions:");
+ (NSRegularExpression *)bdp_generateRegexWithRegexOptions:(BDPRegexOptions)options;
/**
* @brief 使用系统封装的正则 NSDataDetector (NSRegularExpression的子类)
*
* @param checkingTypes 建议只使用 NSTextCheckingTypeDate | NSTextCheckingTypeAddress | NSTextCheckingTypeLink(链接) | NSTextCheckingTypePhoneNumber | NSTextCheckingTypeTransitInformation(交通信息如航班)
* @return NSDataDetector
*/
+ (NSDataDetector *)bba_generateDataDetectorWithTypes:(NSTextCheckingTypes)checkingTypes BDP_FOUNDATION_DEPRECATED("please use bdp_generateDataDetectorWithTypes:");
+ (NSDataDetector *)bdp_generateDataDetectorWithTypes:(NSTextCheckingTypes)checkingTypes;
@end