flpq_data.queries.rpq.readwrite.regex#
Read (and write) a regular expression from (and to) different sources.
Functions
|
Create a regular expression [1] from text. |
|
Create a regular expression [1] from TXT file. |
|
Turns a regular expression [1] into its canonical text representation. |
|
Saves a regular expression [1] text representation into TXT file. |
- regex_from_text(text: str) Regex[source]#
Create a regular expression [1] from text.
The text uses the dataset’s canonical regular expression syntax:
a symbol is a maximal run of characters outside the metacharacter set (an edge label or a nonterminal);
concatenation is whitespace, union is
|, grouping is parentheses;*and+are postfix Kleene star and Kleene plus (X+is sugar forX X*);$is the empty word (epsilon).
Precedence: postfix operators > concatenation > union.
- Parameters:
- textstr
The text with which the regular expression will be created.
- Returns:
- regexRegex
Regular expression.
- Raises:
- ValueError
If the text is empty or is not a well-formed expression (unbalanced parentheses, dangling operators).
References
Examples
>>> from flpq_data import * >>> regex = regex_from_text("a (bc|d*)") >>> regex_to_text(regex) 'a (bc | d*)' >>> regex = regex_from_text("a+") >>> regex.accepts(["a"]) True >>> regex.accepts([]) False
- regex_from_txt(path: Path | str) Regex[source]#
Create a regular expression [1] from TXT file.
- Parameters:
- pathUnion[Path, str]
The path to the TXT file with which the regular expression will be created.
- Returns:
- regexRegex
Regular expression.
References
Examples
>>> from flpq_data import * >>> regex_1 = regex_from_text("a (bc|d*)") >>> path = regex_to_txt(regex_1, "test.txt") >>> regex = regex_from_txt(path) >>> regex_to_text(regex) 'a (bc | d*)'
- regex_to_text(regex: Regex) str[source]#
Turns a regular expression [1] into its canonical text representation.
The output follows the canonical syntax of
regex_from_text()with minimal parentheses, soregex_from_text(regex_to_text(r))is equivalent torandregex_to_textis idempotent.- Parameters:
- regexRegex
Regular expression.
- Returns:
- textstr
Regular expression text representation.
References
Examples
>>> from flpq_data import * >>> regex = regex_from_text("a (bc|d*)") >>> regex_to_text(regex) 'a (bc | d*)'
- regex_to_txt(regex: Regex, path: Path | str) Path[source]#
Saves a regular expression [1] text representation into TXT file.
- Parameters:
- regexRegex
Regular expression.
- pathUnion[Path, str]
The path to the TXT file where regular expression text representation will be saved.
- Returns:
- pathPath
The path to the TXT file where regular expression text representation will be saved.
References
Examples
>>> from flpq_data import * >>> regex = regex_from_text("a (bc|d*)") >>> path = regex_to_txt(regex, "test.txt")