string_matcher v0.1.1 StringMatcher View Source

StringMatcher 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 and . so that we can loop over the long text, as our matches only returns the first match back.

Then you would do:

StringMatcher.new()
|> StringMatcher.add_regexp(~r/Del +(?<episode_num>[0-9]+?) +av +(?<of_episodes>[0-9]+?)/i,  %{})
|> StringMatcher.add_regexp(~r/Originaltitel: +(?<original_title>.*?)/i,  %{})
|> StringMatcher.add_regexp(~r/Produktion: +(?<producer>.*?) (?<episode_num>[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"}

Link to this section Summary

Functions

Add a regexp to the list if its the correct format.

Match a string to a regexp.

Match a string to a regexp.

Create a new list for strings

Link to this section Functions

Link to this function

add_regexp(list, regexp, result)

View Source

Add a regexp to the list if its the correct format.

Returns a list of regular expressions.

Examples

iex> StringMatcher.add_regexp([], ~r/S(?+)E(?+)/i, %{}) [{~r/S(?+)E(?+)/i, %{}}]

Match a string to a regexp.

Returns the values that are passed as the second argument.

Examples

iex> StringMatcher.add_regexp([], ~r/S(?<season_num>+)E(?<episode_num>+)/i, %{}) |> StringMatcher.match("Prison Break E01")
{:error, "no match"}

iex> StringMatcher.add_regexp([], ~r/S(?<season_num>+)E(?<episode_num>+)/i, %{}) |> StringMatcher.match_captures("Prison Break S01E01")
{:ok, %{"episode_num" => "01", "season_num" => "01"}}

iex> StringMatcher.add_regexp([], ~r/S(?<season_num>+)E(?<episode_num>+)/i, %{"name" => "Fargo"}) |> StringMatcher.match("Prison Break S01E01")
{:ok, %{"name" => "Fargo"}}
Link to this function

match_captures(list, string)

View Source

Match a string to a regexp.

Returns either the values passed as the second argument otherwise it returns the captures.

Examples

iex> StringMatcher.add_regexp([], ~r/S(?<season_num>+)E(?<episode_num>+)/i, %{}) |> StringMatcher.match_captures("Prison Break S01E01")
{:ok, %{"episode_num" => "01", "season_num" => "01"}}

iex> StringMatcher.add_regexp([], ~r/S(?<season_num>+)E(?<episode_num>+)/i, %{"name" => "Fargo"}) |> StringMatcher.match_captures("Prison Break S01E01")
{:ok, %{"name" => "Fargo"}}

Create a new list for strings

Returns []

Examples

iex> StringMatcher.new()
[]