正则表达式 STO单号规则

前端之家收集整理的这篇文章主要介绍了正则表达式 STO单号规则前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

STO APP项目 单号规则

其实单号规则很容易写的 开始我也不太了解正则表达式 我只是看了一下安卓代码 然后写了个OC版本的 一次成功

代码如下 就一个CodeMatches类

  1. #import <Foundation/Foundation.h>
  2.  
  3. //单号规则
  4. @interface CodeMatches : NSObject
  5.  
  6. + (NSMutableArray *)startsWithFor12; //12位运单号 规则
  7. + (NSMutableArray *)startsWithFor13; //13位运单号 规则
  8. + (BOOL)matches:(NSString *)billcode;//传入运单号 进行规则对比 是否符合单号规则
  9. + (BOOL)matchesPackeg:(NSString *)pacCode; //传入集包单号 进行规则对比 是否符合单号规则
  10. + (BOOL )startWith:(NSString *)start billcode:(NSString *)billcode; //进行对比
  11.  
  12. @end


  1. #import "CodeMatches.h"
  2.  
  3. @implementation CodeMatches
  4.  
  5. + (BOOL)matches:(NSString *)billcode{
  6. if(billcode == nil){
  7. return NO;
  8. }
  9. if (![[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"\\d+"] evaluateWithObject:billcode] ) {
  10. return NO;
  11. }
  12. BOOL matches = NO;
  13. switch (billcode.length){
  14. case 12:{
  15. for(int i=0; i<[[CodeMatches startsWithFor12] count]; i++){
  16. if([CodeMatches startWith:[CodeMatches startsWithFor12][i] billcode:billcode]){
  17. matches = YES;
  18. break;
  19. }
  20. }
  21. break;
  22. }
  23. case 13:{
  24. for(int i=0; i<[[CodeMatches startsWithFor13] count]; i++){
  25. if([CodeMatches startWith:[CodeMatches startsWithFor13][i] billcode:billcode]){
  26. matches = YES;
  27. break;
  28. }
  29. }
  30. break;
  31. }
  32. }
  33. return matches;
  34. }
  35. + (BOOL)matchesPackeg:(NSString *)pacCode{
  36.  
  37. if(pacCode == nil){
  38. return NO;
  39. }
  40. if (![[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"\\d+"] evaluateWithObject:pacCode] ) {
  41. return NO;
  42. }
  43. BOOL matches = NO;
  44. switch (pacCode.length){
  45. case 12:{
  46. if ([[pacCode substringToIndex:3] isEqualToString:@"900"]) {
  47. matches = YES;
  48. }
  49. break;
  50. }
  51. }
  52. return matches;
  53. }
  54.  
  55. + (NSMutableArray *)startsWithFor12{
  56.  
  57. static NSMutableArray *array = nil;
  58. if (array == nil) {
  59. array = [NSMutableArray arrayWithObjects:@"268",@"368",@"468",@"568",@"768",@"868",@"968",@"40",@"588",@"688",@"888",@"11",@"22",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",nil];
  60. }
  61. return array;
  62. }
  63.  
  64. + (NSMutableArray *)startsWithFor13{
  65. static NSMutableArray *array = nil;
  66. if (array == nil) {
  67. array = [NSMutableArray arrayWithObjects:@"33",@"55",@"660",@"661",@"662",@"663",@"664",@"665",@"666",@"667",@"669",@"77",@"880",@"881",@"882",@"883",@"884",@"885",@"886",@"887",@"889",@"99",nil];
  68. }
  69. return array;
  70. }
  71.  
  72. - (NSString *)matches{
  73. return @"\\d+";
  74. }
  75.  
  76. + (BOOL )startWith:(NSString *)start billcode:(NSString *)billcode{
  77. if (start.length == 2) {
  78. if([[billcode substringToIndex:2]isEqualToString:start]){
  79. return YES;
  80. }else{
  81. return NO;
  82. }
  83. }
  84. if (start.length == 3) {
  85. if([[billcode substringToIndex:3]isEqualToString:start]){
  86. return YES;
  87. }else{
  88. return NO;
  89. }
  90. }
  91. return YES;
  92. }
  93.  
  94. @end

猜你在找的正则表达式相关文章