StringMatcher (string_matcher v0.1.3) 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 \n
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\s+(?<episode_num>[0-9]+?)\s+av\s+(?<of_episodes>[0-9]+?)/i,
%{}
)
|> StringMatcher.add_regexp(~r/Originaltitel: (?<original_title>.*)\./i, %{})
|> StringMatcher.add_regexp(
~r/Produktion: (?<production_company>.*?) (?<production_year>[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"}
Please take a look at our tests to see a working variant of parsing the text above.
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
Add a regexp to the list if its the correct format.
Returns a list of regular expressions.
Examples
iex> StringMatcher.add_regexp([], ~r/S(?<season_num>+)E(?<episode_num>+)/i, %{})
[{~r/S(?<season_num>+)E(?<episode_num>+)/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"}}
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()
[]