Reference
BooleanParser
Bases: TextParser
Convert "TRUE" or "FALSE" (with any case combination) to boolean objects.
Examples
Source code in regexparser/regexparser.py
NumberParser
Bases: TextParser
Convert text with numbers to int and float objects.
Examples
Source code in regexparser/regexparser.py
parseInteger(text, match)
parse_number_decimal(text, match)
PortugueseRulesParser
Bases: TextParser
Convert text to float and boolean according to Brazilian Portuguese conventions.
Examples
>>> parser.parse('1,1')
1,1
>>> parser.parse('- 1.000,1')
-1000.1
>>> parser.parse('Sim')
True
>>> parser.parse('Não')
False
Source code in regexparser/regexparser.py
parseBoolean_ptBR(text, match)
parseBoolean_ptBR2(text, match)
parse_number_decimal_ptBR(text, match)
parse_number_with_thousands_ptBR(text, match)
TextParser
Builds parsers based on regular expressions.
The regular expression, used to match the text pattern, is specified in the
method's __doc__
attribute.
The name of these parser methods must start with parser
.
The parser class inherits TextParser and implements the parser methods
defining the regular expression in the method's __doc__
.
The parser method has two arguments, the first is the given text that is parsed and the second is one instance of the re.Match class. The regular expression attributes can be accessed in this argument. The parser method is called once the regular expression returns a valid Match object.
If one regular expression matches the given text, the method associated to that regular expression is executed, the given text is parsed according to its implementation and the parsed value is returned. Otherwise, the passed text is returned without changes.
Examples
Creat a class to parse integers.
>>> class IntParser(TextParser):
>>> def parseInteger(self, text, match):
>>> r'^-?\s*\d+$'
>>> return eval(text)
Instanciate and call the parse method to convert the given text.
Source code in regexparser/regexparser.py
buildparser(regex, func)
Builds a parser that parses a given text according to regex and func.
Parameters
str
Regular expression to match the desired pattern.
Callable[[str, re.Match], Any]
Function that parses the given text once it matches the regular expression.
Returns
Callable[[str], Any] Returns a callable object that receives the text to be parsed and returns the result of the parsing.
Examples
>>> num_parser = textparser.buildparser(r'^-?\s*\d+[\.,]\d+?$', lambda t, m: eval(t.replace(',', '.')))
>>> num_parser('1.1')
1.1
Source code in regexparser/regexparser.py
textparse(text, regex, func)
Parses the argument text with the function func once it matches the regular expression regex.
Parameters
str
Given text to be parsed.
str
Regular expression to match the desired pattern.
Callable[[str, re.Match], Any]
Function that parses the given text once it matches the regular expression.
Returns
Any Returns the parsed object as result of the parsing.
Examples
>>> textparser.textparse('TRUe', r'^[Tt][Rr][Uu][eE]|[Ff][Aa][Ll][Ss][Ee]$', lambda t, m: eval(t.lower().capitalize()))
True
>>> textparser.textparse('1,1', r'^-?\s*\d+[\.,]\d+?$', lambda t, m: eval(t.replace(',', '.')))
1.1