Iphone 正则表达式以匹配iOS中的数字范围
我是iOS的新程序员,在我正在开发的应用程序中,我需要创建一个正则表达式来匹配给定两个数字的数字范围 我发现这个答案(在Java中)基本上满足了我的需要。 我已将代码转换为Objective-C,当将结果与此站点进行比较时,结果不匹配,可能是我在某个地方犯了错误,或者代码工作不正常 你们中有谁知道我在哪里可以找到类似的东西,但在Objective-C中 这是我的转换代码:Iphone 正则表达式以匹配iOS中的数字范围,iphone,ios,regex,Iphone,Ios,Regex,我是iOS的新程序员,在我正在开发的应用程序中,我需要创建一个正则表达式来匹配给定两个数字的数字范围 我发现这个答案(在Java中)基本上满足了我的需要。 我已将代码转换为Objective-C,当将结果与此站点进行比较时,结果不匹配,可能是我在某个地方犯了错误,或者代码工作不正常 你们中有谁知道我在哪里可以找到类似的东西,但在Objective-C中 这是我的转换代码: -(NSString *) BaseRange:(NSString*)num Bool:(BOOL)up Bool
-(NSString *) BaseRange:(NSString*)num Bool:(BOOL)up BoolLeading:(BOOL)leading1{
unichar c = [num characterAtIndex:0];
unichar low;
unichar high;
if(up == YES){
low=c;
}else{
if (leading1==YES){
low = '1';
}else{
low='0';
}
}
if(up==YES){
high ='9';
}else{
high = c;
}
if(num.length ==1)
return [self charClass:low Other:high];
NSString *re = [NSString stringWithFormat:@"%hu (%@)", c, [self BaseRange:[num substringToIndex:1] Bool:up BoolLeading:NO]];
if(up)
{
low++;
}else{
high--;
}
if(low <= high){
NSString * temp = [NSString stringWithFormat:@"%@|%@%@",re, [self charClass:low Other:high], [self nDigits:[num length] -1]];
re = temp;
}
return re;
}
-(NSString *) charClass:(unichar)b Other:(unichar)e{
if (b==e) {
return [NSString stringWithFormat:@"%c", b];
}
else if(b-e>1)
{
return [NSString stringWithFormat:@"[%c-%c]", b, e];
}
else
{
return [NSString stringWithFormat:@"[%c%c]", b, e];
}
}
-(NSString *) nDigits:(int)n{
return [self nDigits:n otherDigit:n];
}
-(NSString *) nDigits:(int)n otherDigit:(int)m{
if (n==m) {
return @"[0-9]";
}else if (n==1){
return [NSString stringWithFormat:@"[0-9]{%d}", n];
}else{
return [NSString stringWithFormat:@"[0-9]{%d,%d}", n,m];
}
}
-(NSString *) eqLengths:(NSString *)from OtherString:(NSString*)to{
unichar fc = [from characterAtIndex:0];
unichar tc = [to characterAtIndex:0];
if(from.length == 1 && to.length == 1){
return [self charClass:fc Other:tc];
}
if(fc == tc){
return [NSString stringWithFormat:@"%hu(%@)",fc,[self rangeRegex:[from substringToIndex:1] otherValue:[to substringToIndex:1]]];
}
NSString *re = [NSString stringWithFormat:@"%hu(%@)|%hu(%@)",fc,[self BaseRange:[from substringFromIndex:1] Bool:YES BoolLeading:NO],tc,[self BaseRange:[to substringFromIndex:1] Bool:NO BoolLeading:NO]];
if (++fc <=--tc) {
re = [NSString stringWithFormat:@"%@|%@%@", re,[self charClass:fc Other:tc],[self nDigits:[from length]-1]];
}
return re;
}
-(NSString *) nonEqLengths:(NSString *)from OtherString:(NSString *)to{
NSString *re = [NSString stringWithFormat:@"%@|%@",[self BaseRange:from Bool:YES BoolLeading:NO],[self BaseRange:to Bool:NO BoolLeading:YES]];
if ([to length] - [from length] > 1)
re = [NSString stringWithFormat:@"%@|[1-9]%@",re,[self nDigits:[from length] otherDigit:[to length]-2]];
//re += "|[1-9]" + nDigits([from length], [to length] - 2);
return re;
}
-(NSString *) rangeRegex:(NSString *)n otherValue:(NSString *)m{
if(n.length == m.length)
{
return [self eqLengths:n OtherString:m];
}else{
return [self nonEqLengths:n OtherString:m];
}
}
-(NSString*)基本范围:(NSString*)num Bool:(Bool)up boolleding:(Bool)leading 1{
unichar c=[num characterAtIndex:0];
尤尼卡低;
尤尼哈尔高地;
如果(向上==是){
低=c;
}否则{
如果(leading1==是){
低='1';
}否则{
低='0';
}
}
如果(向上==是){
高class='9';
}否则{
高=c;
}
如果(num.length==1)
返回[自身类别:低其他:高];
NSString*re=[NSString StringWith格式:@”%hu(%%)”,c,[自基带:[num substringToIndex:1]Bool:up boolleding:否];
如果(向上)
{
低++;
}否则{
高--;
}
如果(低1)
{
返回[NSString stringWithFormat:@“[%c-%c]”,b,e];
}
其他的
{
返回[NSString stringWithFormat:@“[%c%c]”,b,e];
}
}
-(NSString*)ndigit:(int)n{
返回[self-ndigit:n其他数字:n];
}
-(NSString*)ndigit:(int)n其他数字:(int)m{
如果(n==m){
返回“[0-9]”;
}else如果(n==1){
返回[NSString stringWithFormat:@“[0-9]{%d}”,n];
}否则{
返回[NSString stringWithFormat:@“[0-9]{%d,%d}”,n,m];
}
}
-(NSString*)eqlength:(NSString*)从其他字符串:(NSString*)到{
unichar fc=[来自characterAtIndex:0];
unichar tc=[to characterAtIndex:0];
if(from.length==1&&to.length==1){
返回[self-charClass:fc-Other:tc];
}
如果(fc==tc){
返回[NSString stringWithFormat:@“%hu(%@)”,fc,[self-rangeRegex:[从子字符串到索引:1]其他值:[到子字符串到索引:1]];
}
NSString*re=[NSString stringWithFormat:@“%hu(%@)|%hu(%@)”,fc,[self-BaseRange:[子字符串FromIndex:1]Bool:YES boolleding:NO],tc,[self-BaseRange:[子字符串FromIndex:1]Bool:NO boolleding:NO];
如果(++fc 1)
re=[NSString stringWithFormat:@“%@[1-9]@”,re,[self-nDigits:[from-length]otherDigit:[to-length]-2];
//re+=“|[1-9]”+ndigit([从长度],[到长度]-2);
返回re;
}
-(NSString*)rangeRegex:(NSString*)n其他值:(NSString*)m{
如果(n.length==m.length)
{
返回[self eqLengths:n OtherString:m];
}否则{
返回[self-noneqlength:n其他字符串:m];
}
}
结果:
范围100-1000
我的代码的结果:49([19])|[29][0-9]| 49([01])
网站的预期结果:([1-9][0-9]{2}1000)
范围0-5000
我的代码的结果:[09]| 53([05])|[14][0-9]|[1-9][0-9]{1}
网站的预期结果:^([0-9]{1,3}|[1-4][0-9]{3}|5000)$您是否查看了使用
NSRegularExpression
?您应该展示一些示例,说明您投入的内容、收获的内容与预期收获的内容。这应该有助于您理解,回答了我的问题@Peter M,并对搜索输入和搜索结果进行了编辑