Objective-c 正则表达式格式信用卡 [英] Objective-c Regex format creditcard
本文介绍了Objective-c 正则表达式格式信用卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个卡号列表 1234123412341234,但我需要一个正则表达式,用诸如123 123 123 123"之类的空格对其进行格式化.
I have a list of card numbers 1234123412341234 but I need a regex that formats it with whitespaces like "123 123 123 123".
所以每 4 个字符添加一个空格.
So every 4 character add a whitespace.
推荐答案
以下是如何使用 Ruby(已测试):
Here's how to do it with Ruby (tested):
s = '1234123412341234'
s.sub!(/(\d{4})(\d{4})(\d{4})(\d{4})/, '\1 \2 \3 \4')
print s # => "1234 1234 1234 1234"
我不是 Obj-C 老兄,但使用 RegexKit 或 RegexKit Lite,代码应该(未经测试)类似于:
I'm no Obj-C dude, but with RegexKit or RegexKit Lite, the code should be (untested) something like:
[string stringByMatching:@"(\d{4})(\d{4})(\d{4})(\d{4})" replace:1 withString:@"$1 $2 $3 $4"]
更多阅读:
- 简述正则表达式
- RegexKit 和 RegexKit 精简版
- NSRegularExpression(iOS仅限 4 及以上)
- Regex in a Nutshell
- RegexKit and RegexKit Lite
- NSRegularExpression (iOS 4 and above only)
这篇关于Objective-c 正则表达式格式信用卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文