MOBFRegex.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // MOBFRegex.h
  3. // MOBFoundation
  4. //
  5. // Created by vimfung on 15-1-20.
  6. // Copyright (c) 2015年 MOB. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. /**
  10. * 替换处理
  11. *
  12. * @param captureCount 捕获数量
  13. * @param capturedStrings 捕获字符串集合
  14. * @param capturedRanges 捕获字符串范围集合
  15. * @param stop 是否停止捕获标识
  16. *
  17. * @return 替换后的字符串
  18. */
  19. typedef NSString *(^MOBFReplacingOccurrencesHandler) (NSInteger captureCount, NSString *const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop);
  20. /**
  21. 正则表达式选项
  22. - MOBFRegexOptionsNoOptions: 无匹配
  23. - MOBFRegexOptionsCaseless: 不区分字母大小写的模式
  24. - MOBFRegexOptionsComments: 忽略掉正则表达式中的空格和#号之后的字符
  25. - MOBFRegexOptionsIgnoreMetacharacters: 将正则表达式整体作为字符串处理
  26. - MOBFRegexOptionsDotAll: 允许.匹配任何字符,包括换行符
  27. - MOBFRegexOptionsMultiline: 允许^和$符号匹配整段文本的开头和结尾
  28. - MOBFRegexOptionsUseUnixLineSeparators: 设置\n为唯一的行分隔符,否则所有的都有效。
  29. - MOBFRegexOptionsUnicodeWordBoundaries: 使用Unicode TR#29标准作为词的边界,否则所有传统正则表达式的词边界都有效
  30. */
  31. typedef NS_ENUM(NSUInteger, MOBFRegexOptions)
  32. {
  33. MOBFRegexOptionsNoOptions = 0,
  34. MOBFRegexOptionsCaseless = 1 << 0,
  35. MOBFRegexOptionsComments = 1 << 1,
  36. MOBFRegexOptionsIgnoreMetacharacters = 1 << 2,
  37. MOBFRegexOptionsDotAll = 1 << 3,
  38. MOBFRegexOptionsMultiline = 1 << 4,
  39. MOBFRegexOptionsUseUnixLineSeparators = 1 << 5,
  40. MOBFRegexOptionsUnicodeWordBoundaries = 1 << 6,
  41. };
  42. /**
  43. * 正则表达式工具类
  44. */
  45. @interface MOBFRegex : NSObject
  46. /**
  47. * 替换字符串
  48. *
  49. * @param regex 正则表达式
  50. * @param string 原始字符串
  51. * @param block 块回调处理替换规则
  52. *
  53. * @return 字符串
  54. */
  55. + (NSString *)stringByReplacingOccurrencesOfRegex:(NSString *)regex
  56. withString:(NSString *)string
  57. usingBlock:(MOBFReplacingOccurrencesHandler)block;
  58. /**
  59. * 匹配字符串
  60. *
  61. * @param regex 正则表达式
  62. * @param options 表达式选项
  63. * @param range 匹配范围
  64. * @param string 原始字符串
  65. *
  66. * @return YES 匹配,NO 不匹配
  67. */
  68. + (BOOL)isMatchedByRegex:(NSString *)regex
  69. options:(MOBFRegexOptions)options
  70. inRange:(NSRange)range
  71. withString:(NSString *)string;
  72. /**
  73. * 匹配字符串
  74. *
  75. * @param regex 正则表达式
  76. * @param string 原始字符串
  77. *
  78. * @return 匹配的字符串集合
  79. */
  80. + (NSArray *)captureComponentsMatchedByRegex:(NSString *)regex
  81. withString:(NSString *)string;
  82. @end