Detect double quoted sentences in text, using regular expressions

Viewed 28

I am making an application in Swift where I use the ActiveLabel library, this library allows you to detect links and emails in UILabel. https://github.com/optonaut/ActiveLabel.swift

Within the functions of this library is the use of custom patterns to detect other elements in the text. I want to be able to detect sentence that are between double quotes.

I am using this regular expressions: "(?<=“).*(?=”)" and it shows me the following: image quotes

    struct RegexParser {

    static let quotePattern = "(?<=“).*(?=”)"
    static let emailPattern = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
    static let urlPattern = "(^|[\\s.:;?\\-\\]<\\(])" +
        "((https?://|www\\.|pic\\.)[-\\w;/?:@&=+$\\|\\_.!~*\\|'()\\[\\]%#,☺]+[\\w/#](\\(\\))?)" +
    "(?=$|[\\s',\\|\\(\\).:;?\\-\\[\\]>\\)])"

    private static var cachedRegularExpressions: [String : NSRegularExpression] = [:]

    static func getElements(from text: String, with pattern: String, range: NSRange) -> [NSTextCheckingResult]{
        guard let elementRegex = regularExpression(for: pattern) else { return [] }
        return elementRegex.matches(in: text, options: [], range: range)
    }

    private static func regularExpression(for pattern: String) -> NSRegularExpression? {
        if let regex = cachedRegularExpressions[pattern] {
            return regex
        } else if let createdRegex = try? NSRegularExpression(pattern: pattern, options: [.caseInsensitive]) {
            cachedRegularExpressions[pattern] = createdRegex
            return createdRegex
        } else {
            return nil
        }
    }
}

What I want: What I want

As you can see, it detects everything as a single sentence between quotes, while in reality there are two sentences between quotes in the same paragraph.

How can I make it to detect the two sentences between quotes separately?

0 Answers
Related