Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jnylen/string-matcher
An Elixir Library to run several regular expressions on a string and get values back.
https://github.com/jnylen/string-matcher
elixir elixir-lang elixir-library regex regular-expression string-matching string-parsing
Last synced: about 2 months ago
JSON representation
An Elixir Library to run several regular expressions on a string and get values back.
- Host: GitHub
- URL: https://github.com/jnylen/string-matcher
- Owner: jnylen
- License: mit
- Created: 2020-02-04T17:21:53.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-08-28T04:37:00.000Z (over 1 year ago)
- Last Synced: 2024-11-22T22:09:03.245Z (3 months ago)
- Topics: elixir, elixir-lang, elixir-library, regex, regular-expression, string-matching, string-parsing
- Language: Elixir
- Homepage:
- Size: 26.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StringMatcher
This library allows you to pass multiple regular expressions and a string and get values back.
## Example
Let's say you have a text that is:
```
Del 5 av 6. Shakespeare är mycket nöjd med sin senaste pjäs, Så tuktas en argbigga. Men av någon anledning uppskattas inte berättelsen om hur en stark kvinna förnedras av en man av kvinnorna i Shakespeares närhet.Originaltitel: Upstart Crow.
Produktion: BBC 2017.
```First we would split the text into an array based on `\n` and `.` so that we can loop over the long text, as our matches only returns the first match back.
Then you would do:
```elixir
StringMatcher.new()
|> StringMatcher.add_regexp(
~r/Del\s+(?[0-9]+?)\s+av\s+(?[0-9]+?)/i,
%{}
)
|> StringMatcher.add_regexp(~r/Originaltitel: (?.*)\./i, %{})
|> StringMatcher.add_regexp(
~r/Produktion: (?.*?) (?[0-9]+)\./i,
%{}
)
|> StringMatcher.match_captures(string)
```This should return a tuple with a map. The map is returned value of the regular expressions.
If no match is found you will receive `{:error, "no match"}`## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `string_matcher` to your list of dependencies in `mix.exs`:```elixir
def deps do
[
{:string_matcher, "~> 0.1.0"}
]
end
```Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/string_matcher](https://hexdocs.pm/string_matcher).## Tests
Currently the tests are failing for some reason, the library is working though and is stable.
It's used in production.