import Foundation
let pattern = #"(?:\[device name\]:\[?|\[name_of_device\]- |,)([^\],\r\n]*)(?=.*\r?\n(?:[ \t]*\[.*\r?\n)*?[ \t]*\[(?:current software\]: |software_running\]- )(.*))"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = ##"""
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. This is a list of all kinds of models, software, and release dates. See below for more details
{
#device batch 1 info can be found below
{
[device name]:device_1
[initial software]: SW$1_978qhuhk
[current software]: sW98203_973
[date] : 2022-01-05
}
{
[device name]:[device_2, device_3]
[initial software]: sW$3_jghs
[current software]: sW$3_jghs2
[date] : 2022-07-03
}
#wifi devices can be found here
{
{
"nested children":
[device name]:[w_device_1, w_device_pro]
[initial software]: 982637012_sw
[current software]: 93834932_swp$
[date] : 2024-02-01
}
{
[name_of_device]- mesh_plus
[installation_date]-2023-03-21
[software_running]- mesh_sw_old
}
}
}
"""##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #""#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression