我正在尝试编写一个返回true或false的函数,如果它是一个有效的比特币地址。
我目前有一个QR码扫描程序,它返回如下字符串:
比特币:18YCdhPsd5gdf6WD4ioMZZJKTNdajUJyTM
这是我的功能:
func isValidBitcoinAddress() -> Bool {
var fullAddress = self.components(separatedBy: ":")
let r = fullAddress[1].startIndex..<fullAddress[1].endIndex
let pattern = "^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$"
let r2 = self.range(of: pattern, options: .regularExpression)
if r2 == r {
return true
} else {
return false
}
}
不幸的是,每次都会返回false。
我从这个网站获得了RegEx模式:http://mokagio.github.io/tech-journal/2014/11/21/regex-bitcoin.html
Swift中的格式可能不同吗?
谢谢!
答案 0 :(得分:1)
您需要仅在 Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
repeater1.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
:
:
答案 1 :(得分:1)
我只是添加了我实现的代码:
func isValidBitcoinAddress(stringBitCoinID:String) -> Bool {
let fullAddress = stringBitCoinID.components(separatedBy: ":")
guard fullAddress.count == 2, fullAddress[0] == "bitcoin" else {
return false
}
let r = fullAddress[1]
let pattern = "^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$"
let bitCoinIDTest = NSPredicate(format:"SELF MATCHES %@", pattern)
let result = bitCoinIDTest.evaluate(with: r)
return result
}
答案 2 :(得分:0)
您正在将self
与模式进行比较,而不是fullAddress[1]
。
使用以下内容:
func isValidBitcoinAddress() -> Bool {
let fullAddress = self.components(separatedBy: ":")
if fullAddress.count == 2 && fullAddress[0] == "bitcoin" {
let pattern = "^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$"
let r = fullAddress[1].startIndex..<fullAddress[1].endIndex
let r2 = fullAddress[1].range(of: pattern, options: .regularExpression)
return r == r2
} else {
return false
}
}