用于验证利率格式 Swift3 的正则表达式 [英] Regular Expression to validate Rate of Interest Format Swift3

查看:36
本文介绍了用于验证利率格式 Swift3 的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为我的 TextField 之一处理正则表达式,以只允许利率格式的数字.

I have been trying to work on Regular Expressions for one of my TextField to only allow digits in Rate of Interest format.

我希望该字段允许在."之前输入最多 2 位数字.字符和最多 2 个数字.

I want the field to allow input of max 2 digits before "." character and max 2 digits after.

有效格式:-

1、10、1.2、11.4、15.24

1, 10, 1.2, 11.4, 15.24

并且该字段的最大长度为 5 个字符

And Max length of the field to be 5 characters

尝试了一些,但没有任何效果.

Tried few but nothing worked out.

请帮忙.提前致谢.

推荐答案

试试下面的代码

let string = "10.55"

if let match = string.range(of: "^\\d{1,2}(\\.\\d{1,2})?$", options: .regularExpression) {
    print("Match")
}

或者你也可以使用guard

Or you can use guard too

guard let match = string.range(of: "^\\d{1,2}(\\.\\d{1,2})?$", options: .regularExpression) else {
    return
}

这篇关于用于验证利率格式 Swift3 的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆