如何让正则表达式从目标 c 字符串中获取所有字母?
我试图使用以下正则表达式来仅获取字母数字字符输入框中的字母,但它始终返回完整字符串,而不是任何 AZ 字母。
我做错了什么?
它只需要抓取所有字母。没有奇怪的字符,没有数字,只有 AZ 并将其放入字符串中供我稍后使用。
// A default follows
NSString *TAXCODE = txtTaxCode.text;
// Setup default for taxcode
if ([TAXCODE length] ==0)
{
TAXCODE = @"647L";
}
NSError *error = NULL;
NSRegularExpression *regex;
regex = [NSRegularExpression regularExpressionWithPattern:@"/[^A-Z]/gi"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSLog(@"TAXCODE = %@", TAXCODE);
NSLog(@"TAXCODE.length = %d", [TAXCODE length]);
NSLog(@"STC (before regex) = %@", STC);
STC = [regex stringByReplacingMatchesInString:TAXCODE
options:0
range:NSMakeRange(0, [TAXCODE length])
withTemplate:@""];
NSLog(@"STC (after regex) = %@", STC);
我的调试输出如下:
税号 = 647L
税码.长度=4
STC(正则表达式之前)=
STC(正则表达式后)= 647L
I'm trying to get the following regular expression to grab only the letters from an alpha-numeric character input box, however it's always returning the full string, and not any of the A-Z letters.
What am I doing wrong?
It needs to grab all the letters only. No weird characters and no numbers, just A-Z and put it into a string for me to use later on.
// A default follows
NSString *TAXCODE = txtTaxCode.text;
// Setup default for taxcode
if ([TAXCODE length] ==0)
{
TAXCODE = @"647L";
}
NSError *error = NULL;
NSRegularExpression *regex;
regex = [NSRegularExpression regularExpressionWithPattern:@"/[^A-Z]/gi"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSLog(@"TAXCODE = %@", TAXCODE);
NSLog(@"TAXCODE.length = %d", [TAXCODE length]);
NSLog(@"STC (before regex) = %@", STC);
STC = [regex stringByReplacingMatchesInString:TAXCODE
options:0
range:NSMakeRange(0, [TAXCODE length])
withTemplate:@""];
NSLog(@"STC (after regex) = %@", STC);
My debug output is as follows:
TAXCODE = 647L
TAXCODE.length = 4
STC (before regex) =
STC (after regex) = 647L
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想在一端有字母,那么您可以使用。
如果混合字母,那么您可以获得一个可以使用的数组。
If you only ever going to have letters on one end then you could use.
If intermixed letters then you can get an Array that you can then play with.
我认为你需要在正则表达式上删除 perl 语法。使用@“[^AZ]”作为匹配字符串。
I think you need to drop the perl syntax on the regexp. Use @"[^A-Z]" as the match string.