开发者

Objective-c Regex format creditcard

开发者 https://www.devze.com 2023-02-19 03:46 出处:网络
I have a list of card numbers 1234123412341234 but I need a regex that formats it w开发者_开发技巧ith whitespaces like \"123 123 123 123\".

I have a list of card numbers 1234123412341234 but I need a regex that formats it w开发者_开发技巧ith whitespaces like "123 123 123 123".

So every 4 character add a whitespace.


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"

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"]

Some more reading:

  • Regex in a Nutshell
  • RegexKit and RegexKit Lite
  • NSRegularExpression (iOS 4 and above only)
0

精彩评论

暂无评论...
验证码 换一张
取 消