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)
精彩评论