Swift Requires Chilkat v11.7.0+
Swift
Search a PDF with a Regular Expression
See more PDF Text Extraction Examples
Demonstrates searching a PDF with a regular expression (regex: true), limiting the number of results (maxResults), and matching whole words only (wholeWord).Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at order_records.pdf
let pdf = CkoPdf()!
success = pdf.loadFile(filePath: "qa_data/pdf/order_records.pdf")
if success == false {
print("\(pdf.lastErrorText!)")
return
}
let json = CkoJsonObject()!
let jsonOut = CkoJsonObject()!
// Find every order number. With regex set to true, the query is a regular expression.
json.updateString(jsonPath: "query", value: "ORD-[0-9]{4}-[0-9]{4}")
json.updateBool(jsonPath: "regex", value: true)
// We only want the matched text, so no context is needed.
json.updateInt(jsonPath: "context", value: 0)
success = pdf.extractText(op: "search", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("Order numbers found: \(jsonOut.int(of: "count").intValue)")
var i: Int = 0
var count_i: Int = jsonOut.size(ofArray: "matches").intValue
while i < count_i {
jsonOut.i = i
print("\(jsonOut.string(of: "matches[i].text")!)")
i = i + 1
}
// Find the e-mail addresses, but stop after the first three.
json.updateString(jsonPath: "query", value: "[a-z0-9._]+@[a-z]+[.][a-z]+")
json.updateInt(jsonPath: "maxResults", value: 3)
success = pdf.extractText(op: "search", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("First three e-mail addresses:")
i = 0
count_i = jsonOut.size(ofArray: "matches").intValue
while i < count_i {
jsonOut.i = i
print("\(jsonOut.string(of: "matches[i].text")!)")
i = i + 1
}
// wholeWord: true matches only whole words. The word "cat" appears once in the PDF,
// but it is also inside "catalog" and "category".
json.delete(name: "maxResults")
json.updateBool(jsonPath: "regex", value: false)
json.updateString(jsonPath: "query", value: "cat")
success = pdf.extractText(op: "search", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("Occurrences of cat (any): \(jsonOut.int(of: "count").intValue)")
json.updateBool(jsonPath: "wholeWord", value: true)
success = pdf.extractText(op: "search", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("Occurrences of cat (whole word): \(jsonOut.int(of: "count").intValue)")
// Sample output:
// Order numbers found: 9
// ORD-2026-0301
// ORD-2026-0302
// ORD-2026-0303
// ORD-2026-0304
// ORD-2026-0305
// ORD-2026-0306
// ORD-2026-0307
// ORD-2026-0308
// ORD-2026-0307
// First three e-mail addresses:
// alice.moore@example.com
// bkumar@example.org
// carla@example.net
// Occurrences of cat (any): 3
// Occurrences of cat (whole word): 1
}