Skip to content

Repository files navigation

ktsu.TextFilter

A .NET library for filtering text using glob patterns, regular expressions, and fuzzy matching

LicenseNuGet VersionNuGet VersionNuGet DownloadsGitHub commit activityGitHub contributorsGitHub Actions Workflow Status

Introduction

ktsu.TextFilter is a .NET library that provides methods for filtering text based on different filter types and match options. It supports glob patterns, regular expressions, and fuzzy matching to help you efficiently filter and search through collections of strings.

Features

  • Glob Pattern Matching: Filter text using glob patterns with optional, required, and excluded tokens.
  • Regular Expression Matching: Filter text using regular expressions.
  • Fuzzy Matching: Rank text based on how well it matches a fuzzy pattern.
  • Customizable Match Options: Match by whole string, all words, or any word.

Installation

Package Manager Console

Install-Package ktsu.TextFilter

.NET CLI

dotnet add package ktsu.TextFilter

Package Reference

<PackageReferenceInclude="ktsu.TextFilter"Version="x.y.z" />

Usage Examples

Basic Example

usingktsu.TextFilter;stringtext="Hello, World!";stringpattern="Hello*";boolisMatch=TextFilter.Match(text,pattern);

Filtering Collections

usingktsu.TextFilter;string[]texts=newstring[]{"Hello, World!","Goodbye, World!"};stringpattern="Hello*";IEnumerable<string>matches=TextFilter.Filter(texts,pattern);

Ranking Results

usingktsu.TextFilter;string[]texts=newstring[]{"Hello, World!","Goodbye, World!"};stringpattern="Hello";IEnumerable<string>ranked=TextFilter.Rank(texts,pattern);

Advanced Usage

Match Options

You can customize how matching is performed using the MatchOptions enum:

usingktsu.TextFilter;stringtext="Hello beautiful world";stringpattern="hello world";// Match by any word in the patternboolanyWordMatch=TextFilter.Match(text,pattern,MatchOptions.AnyWord);// Match by all words in the patternboolallWordsMatch=TextFilter.Match(text,pattern,MatchOptions.AllWords);// Match the entire stringboolwholeStringMatch=TextFilter.Match(text,pattern,MatchOptions.WholeString);

Filter Types

TextFilter supports different filter types:

usingktsu.TextFilter;stringtext="Hello, World!";stringglobPattern="Hello*";stringregexPattern="^Hello";// Glob pattern matchingboolglobMatch=TextFilter.Match(text,globPattern,filterType:FilterType.Glob);// Regular expression matchingboolregexMatch=TextFilter.Match(text,regexPattern,filterType:FilterType.Regex);// Fuzzy matchingboolfuzzyMatch=TextFilter.Match(text,"Helo",filterType:FilterType.Fuzzy);

API Reference

TextFilter Class

The primary class for text filtering operations.

Methods

NameReturn TypeDescription
Match(string text, string pattern, MatchOptions options = MatchOptions.WholeString, FilterType filterType = FilterType.Glob)boolTests if the input text matches the specified pattern
Filter(IEnumerable<string> texts, string pattern, MatchOptions options = MatchOptions.WholeString, FilterType filterType = FilterType.Glob)IEnumerable<string>Returns all texts that match the pattern
Rank(IEnumerable<string> texts, string pattern, MatchOptions options = MatchOptions.WholeString)IEnumerable<string>Returns texts ranked by how well they match the pattern

Enums

MatchOptions

ValueDescription
WholeStringMatch the entire string against the pattern
AllWordsMatch all words in the pattern against the string
AnyWordMatch any word in the pattern against the string

FilterType

ValueDescription
GlobUse glob pattern matching
RegexUse regular expression matching
FuzzyUse fuzzy matching

Contributing

Contributions are welcome! For feature requests, bug reports, or questions, please open an issue on GitHub. If you would like to contribute code, please open a pull request with your changes.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages