swift5 正则表达式入门

2021-11-26  本文已影响0人  Johnson_9d92

swift5 正则表达式入门

//
//  ViewController.swift
//  正则表达式学习
//
//  Created by lujun on 2021/11/25.
//
/**

 
*/
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //匹配abc
//        let str: String = "asdabc12鲁军asdqadqweqwabceadsa"
//        let str: String = "*32a453f423d45t34a"
//包含一个a~z,后面必须是0-9 -- > [a-z][0-9] 或者 [a-z]\d \d是数字对象匹配
//        let pattern = "[a-z][0-9]"
//        let pattern = "[a-z]\\d"
        //首字母必须是a-z某一个字符
//        let pattern = "^[a-z]\\d{2,10}"
        //必须以a-z字母结尾
//        let pattern = "[a-z]$"
        //首字母必须不是0-9
         //        let pattern = "^[^0-9]"
        //        let pattern = "abc"
        //let str = "1231223463"
//  let pattern = "^[1-9]\\d{4,11}$" //是否是QQ号
//电话号码的匹配 正则
        let str = "18895706468"
        let pattern = "^1[3578]\\d{9}$"
        guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
            return
        }
        let results = regex.matches(in: str, options: [], range: NSRange(location: 0, length: str.count))
        for result in results {
            debugPrint( (str as NSString).substring(with: result.range) )
            debugPrint(result.range)
        }
    }
}


上一篇 下一篇

猜你喜欢

热点阅读