Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] - Regex Patterns for filtering #1175

Closed
ondrovic opened this issue Mar 13, 2021 · 2 comments
Closed

[Feature Request] - Regex Patterns for filtering #1175

ondrovic opened this issue Mar 13, 2021 · 2 comments

Comments

@ondrovic
Copy link

ondrovic commented Mar 13, 2021

Would love to see the ability to use regex patterns in the filtering, would make it easier to filter out content.

Examples:
filter to exclude items that have one or two digits in the title
(\b\d\b|\b\d{2}\b)

filter to exclude items that are surround by [] - [EXAMPLE], [10], [etc]
\[\w.+\]

@ondrovic
Copy link
Author

ondrovic commented Mar 13, 2021

so I think I may have come up with a solution to this, I am not very good at swift, or have an apple developer account but did test it locally and it seemed to work. Maybe some who is a bit better can take it an polish it a bit.

I made the following function

public static func patternMatched(_ array: [NSString], value: String) -> Bool {
        for text in array {
            let regex = try! NSRegularExpression(pattern: String(text), options: NSRegularExpression.Options.caseInsensitive)
            let matches = regex.matches(in: value, options: [], range: NSRange(location: 0, length: value.length))
            
            if !matches.isEmpty {
                return true
            }
        }
        
        return false
    }

then used it like this as an example

patternMatched(PostFilter.titles, value: link.title)

I have attached the whole modified file PostFilter.swift this was only done for Slide For Reddit so it may need to be applied to the other projects, applied for title, htmlBody, flairs

PostFilter.zip

@ccrama
Copy link
Collaborator

ccrama commented Mar 27, 2021

Good stuff, thank you for the detailed post and code! I abstracted the logic out a little bit so that the function could be used elsewhere, but I used parts of your solution in 2e38f4d#diff-612caacac62a32d8c8373caa792fac4d39710cae4349a5e974a59b26a0362640R57.

It works great, will be shipping this with v7.1!

@ccrama ccrama closed this as completed Mar 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants