MOBFRegex.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. */
  23. typedef NS_ENUM(NSUInteger, MOBFRegexOptions){
  24. /**
  25. * 无设置
  26. */
  27. MOBFRegexOptionsNoOptions = 0,
  28. /**
  29. * 不区分大小写
  30. */
  31. MOBFRegexOptionsCaseless = 2,
  32. /**
  33. * 注释
  34. */
  35. MOBFRegexOptionsComments = 4,
  36. /**
  37. * 匹配点
  38. */
  39. MOBFRegexOptionsDotAll = 32,
  40. /**
  41. * 多行模式
  42. */
  43. MOBFRegexOptionsMultiline = 8,
  44. /**
  45. * Unicode字符
  46. */
  47. MOBFRegexOptionsUnicodeWordBoundaries = 256
  48. };
  49. /**
  50. * 正则表达式工具类
  51. */
  52. @interface MOBFRegex : NSObject
  53. /**
  54. * 替换字符串
  55. *
  56. * @param regex 正则表达式
  57. * @param string 原始字符串
  58. * @param block 块回调处理替换规则
  59. *
  60. * @return 字符串
  61. */
  62. + (NSString *)stringByReplacingOccurrencesOfRegex:(NSString *)regex
  63. withString:(NSString *)string
  64. usingBlock:(MOBFReplacingOccurrencesHandler)block;
  65. /**
  66. * 匹配字符串
  67. *
  68. * @param regex 正则表达式
  69. * @param options 表达式选项
  70. * @param range 匹配范围
  71. * @param string 原始字符串
  72. *
  73. * @return YES 匹配,NO 不匹配
  74. */
  75. + (BOOL)isMatchedByRegex:(NSString *)regex
  76. options:(MOBFRegexOptions)options
  77. inRange:(NSRange)range
  78. withString:(NSString *)string;
  79. /**
  80. * 匹配字符串
  81. *
  82. * @param regex 正则表达式
  83. * @param string 原始字符串
  84. *
  85. * @return 匹配的字符串集合
  86. */
  87. + (NSArray *)captureComponentsMatchedByRegex:(NSString *)regex
  88. withString:(NSString *)string;
  89. @end