我需要删除训练零的数字,例如: 9.2500 = 9.25 感谢@Avinash Raj的上一篇文章,我发现了这个:
^(\d+\.\d*?[1-9])0+$
哪个适用于该应用程序。但是,在某些情况下,我有一个像11.0000的数字,上面的RegEx返回:
11.0
我需要回复:
11
我不确定如何在没有需要时删除小数和零?我花了一些时间试图解决这个问题,但我很难过。
答案 0 :(得分:3)
试试这个:
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = "id=42".data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
// ...
}
task.resume()
答案 1 :(得分:2)
^(\d+(?:\.\d*?[1-9](?=0|\b))?)\.?0*$
演示here。
<强>解释强>
^ //Beginning of line
( //Start collecting our group
\d+ //All digits before decimal
(?: //START-Non collecting group
\. //Decimal point
\d*? //It should actually be [0-9]
[1-9] //Last significant digit after decimal
(?=0|\b) //Either should be followed by zero or END OF WORD
)? //END-Non collecting group
//The non-capturing group after decimal is optional
) //End collecting our group
\.? //Optional decimal (decimal collected if it wasn't used earlier)
0* //All the remaining zeros no + as all digits might be significant that is no ending zero
$ //End of line.
答案 2 :(得分:1)
试试这个
^\d*\.[1-9]*([0]+)*$
解释
^ beginning of the term
\d* digits from 0-9 those can be any number (can be 0 number or more)
\. escaping the .
[1-9]* numbers from 1 to 9 (can be 0 number or more)
([0]+) capturing all 0s in group1
$ end of the term