model_to_regex: Build a regular expression to fit chosen parameters

Description Usage Arguments Warning See Also Examples

Description

Build a regular expression to fit chosen parameters

Usage

1
2
model_to_regex(model = "*", allow = letters, ban = character(0),
  type = "usual")

Arguments

model

pattern that a word should match. Consists of letters and unknown characters specifications. Dot . stands for unknown character. It may be followed by {...} repetition quantifier (i.e. .{n}, .{n,}, .{n,m}). Asterisk * stands for unknown number of unknown characters. See examples.
By default model is set to "*".

allow

characters allowed to fill gaps in a word. Can be listed in a single string or in a vector. By default is set to letters.

ban

characters not allowed to fill gaps in a word.

type

can be "usual", "scrabble", or "anagram". Abbreviated input is allowed: e.g. "u", "s", or "a".
type defines how often allowed characters can be used to fill the gaps. Say, character appears n times in allow and m times in ban. If d = n - m is less or equal to zero, whatever the type is, this character won't be used to fill the gaps. For the case when d > 0:

  • If type is "usual" then the character is allowed to fill the gaps unlimited number of times.

  • If type is "scrabble" then the character is allowed to fill the gaps no more than d times.

  • If type is "anagram" then the character should be used exactly d times.

Warning

If type = "scrabble" or "anagram", output regex will contain perl-like syntax. So, to use it in grep or gsub for example, set perl parameter to TRUE.

See Also

find_word, scrabble, anagram

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## Regular expression to match all the 5-letter words starting with "c".
model_to_regex("c.{4}")
## Disallow "a" and "b".
model_to_regex("c.{4}", ban = "ab")
## Allow only "a" and "b" to fill the gap.
model_to_regex("c.{4}", allow = "ab")
## Allow "a", "b", and "c", but then ban "c" (result is the same as the previous example)
model_to_regex("c.{4}", allow = "abc", ban = "c")

## Regex to match all words that start with "p" and end with "zed".
model_to_regex("p*zed")

## Regex to match all the words that can be constructed of the word "thing".
model_to_regex(allow = "thing", type = "scrabble")
## Get at lest 4-letter words.
model_to_regex(".{4,}", allow = "thing", type = "scrabble")

## Regex to match anagrams of the word "thing"
model_to_regex(allow = "thing", type = "anagram")

wfindr documentation built on May 2, 2019, 12:20 p.m.