I am looking for a way to identify a character pattern within a character pattern and delete any inner examples of the pattern, if they exist, to leave only the outer ones.
An example would be:
str = "some text [outer part [inner part] back to outer part] more text"
I'd like to delete the inner pattern [ ] to leave:
str = "some text [outer part inner part back to outer part] more text"
This is not always the format. One could also see:
str = "this text [does does no need inner brackets removed] as there aren't any"
str = "this text [does does not] need inner brackets [removed] as there aren't any"
str = "this text [has one [instance] of inner brackets] and another [that is] okay"
Note: if differing open and close delimiters are an issue, I can change them to one delimiter such as a * but I still want to get rid of the inner delimiters.
This seems straightforward but is proving harder than I expected as str_replace doesn't naturally detect which is outer and which is inner. For example, in the following I can find the character [ but not sure how to only delete if it is inside another [...
let string = "some text [outer part [inner part] back to outer part] more text"
if string.range(of: "[\b(?=.*[)[a-zA-Z]{1,8}\b", options: [.regularExpression, caseInsensitive]) != nil {
print("found a match")
} else {
print("no match present")
}
Thanks for any suggestions.