本文整理汇总了Python中pyparsing.Regex方法的典型用法代码示例。如果您正苦于以下问题:Python pyparsing.Regex方法的具体用法?Python pyparsing.Regex怎么用?Python pyparsing.Regex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyparsing
的用法示例。
在下文中一共展示了pyparsing.Regex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _wrap_as_optional_numeric
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def _wrap_as_optional_numeric(self, field, name, columns):
# Regular expression accepting as many whitespaces as columns
field_empty = pp.Regex('[0]{' + str(columns) + '}')
resultsName = field.resultsName
field_empty.setName(name)
# Whitespaces are not removed
field_empty.leaveWhitespace()
# None is returned by this rule
field_empty.setParseAction(pp.replaceWith(None))
field_empty = field_empty.setResultsName(field.resultsName)
field = field | field_empty
field.setName(name)
field = field.setResultsName(resultsName)
field.leaveWhitespace()
return field
示例2: flag
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def flag(name=None):
"""
Creates the grammar for a Flag (F) field, accepting only 'Y', 'N' or 'U'.
:param name: name for the field
:return: grammar for the flag field
"""
if name is None:
name = 'Flag Field'
# Basic field
field = pp.Regex('[YNU]')
# Name
field.setName(name)
field.leaveWhitespace()
return field
示例3: blank
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def blank(columns=1, name=None):
"""
Creates the grammar for a blank field.
These are for constant empty strings which should be ignored, as they are
used just as fillers.
:param columns: number of columns, which is the required number of
whitespaces
:param name: name for the field
:return: grammar for the blank field
"""
if name is None:
name = 'Blank Field'
field = pp.Regex('[ ]{' + str(columns) + '}')
field.leaveWhitespace()
field.suppress()
field.setName(name)
return field
示例4: visan
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def visan(name=None):
"""
Creates the grammar for a V-ISAN code.
This is a variation on the ISAN (International Standard Audiovisual Number)
:param name: name for the field
:return: grammar for an ISRC field
"""
if name is None:
name = 'V-ISAN Field'
field = pp.Regex('[0-9]{25}')
field.setName(name)
return field.setResultsName('visan')
示例5: _parse
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def _parse(mystr):
LBRACE, RBRACE, EQUAL = map(pp.Suppress, "{}=")
field = pp.Word(pp.printables + ' ', excludeChars='[]=')
field.addParseAction(pp.tokenMap(str.rstrip))
string = pp.dblQuotedString().setParseAction(pp.removeQuotes)
number = pp.pyparsing_common.number()
date_expr = pp.Regex(r'\d\d\d\d-\d\d-\d\d')
time_expr = pp.Regex(r'\d\d:\d\d:\d\d\.\d\d\d')
nan = pp.Keyword('nan')
scalar_value = (string | date_expr | time_expr | number | nan)
list_marker = pp.Suppress("[]")
value_list = pp.Forward()
jobject = pp.Forward()
memberDef1 = pp.Group(field + EQUAL + scalar_value)
memberDef2 = pp.Group(field + EQUAL + jobject)
memberDef3 = pp.Group(field + list_marker + EQUAL + LBRACE + value_list +
RBRACE)
memberDef = memberDef1 | memberDef2 | memberDef3
value_list <<= (pp.delimitedList(scalar_value, ",") |
pp.ZeroOrMore(pp.Group(pp.Dict(memberDef2))))
value_list.setParseAction(lambda t: [pp.ParseResults(t[:])])
members = pp.OneOrMore(memberDef)
jobject <<= pp.Dict(LBRACE + pp.ZeroOrMore(memberDef) + RBRACE)
# force empty jobject to be a dict
jobject.setParseAction(lambda t: t or {})
parser = members
parser = pp.OneOrMore(pp.Group(pp.Dict(memberDef)))
return parser.parseString(mystr)
示例6: _cast_transformer
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def _cast_transformer(self):
"""Removes obvious casts."""
return pyparsing.Combine(
pyparsing.Regex(r"\([^()]*\)").suppress()
+ (pyparsing.Word(pyparsing.alphanums + "_")
| pyparsing.Literal("(")),
adjacent=False)
示例7: __init__
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def __init__(self):
"""
Create a parser that parse arithmetic expressions. They can
contains variable identifiers or raw numbers. The meaning
for the identifiers is left to the
"""
number = p.Regex(r'\d+(\.\d*)?([eE]\d+)?')
identifier = p.Word(p.alphas)
terminal = identifier | number
self._expr = p.infixNotation(terminal, [
(p.oneOf('* /'), 2, p.opAssoc.LEFT),
(p.oneOf('+ -'), 2, p.opAssoc.LEFT)
]) + p.stringEnd()
示例8: GetParams
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def GetParams(s):
"""Extracts args and kwargs from string."""
# modified from https://stackoverflow.com/questions/38799223/parse-string-to-identify-kwargs-and-args # pylint: disable=line-too-long
_lparen = Suppress("(") # pylint: disable=invalid-name
_rparen = Suppress(")") # pylint: disable=invalid-name
_eq = Suppress("=") # pylint: disable=invalid-name
data = (_lparen + Optional(
delimitedList(
Group(Regex(r"[^=,)\s]+") + Optional(_eq + Regex(u"[^,)]*")))
)
) + _rparen)
items = data.parseString(s).asList()
# need to make sure that kwargs only happen after args are processed
args = [Num(i[0]) for i in items if len(i) == 1]
kwargs = {i[0]: Num(i[1]) for i in items if len(i) == 2}
# check for syntax error
for i in range(1, len(items)):
if (len(items[i]) == 1) and (len(items[i-1]) == 2):
raise SyntaxError
return args, kwargs
示例9: word_token_regex
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def word_token_regex(disallowed_delimiter):
return pypar.Regex(r"[^\s\n" + re.escape(disallowed_delimiter) + r"]+")
示例10: _wrap_as_optional
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def _wrap_as_optional(self, field, name, columns):
"""
Adds a wrapper rule to the field to accept empty strings.
This empty string should be of the same size as the columns parameter.
One smaller or bigger will be rejected.
This wrapper will return None if the field is empty.
:param field: the field to wrap
:param name: name of the field
:param columns: number of columns it takes
:return: the field with an additional rule to allow empty strings
"""
# Regular expression accepting as many whitespaces as columns
field_empty = pp.Regex('[ ]{' + str(columns) + '}')
resultsName = field.resultsName
field_empty.setName(name)
# Whitespaces are not removed
field_empty.leaveWhitespace()
# None is returned by this rule
field_empty.setParseAction(pp.replaceWith(None))
field_empty = field_empty.setResultsName(resultsName)
field = field | field_empty
field.setName(name)
field = field.setResultsName(resultsName)
field.leaveWhitespace()
return field
示例11: char_code
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def char_code(columns, name=None):
"""
Character set code field.
:param name: name for the field
:return: an instance of the Character set code field rules
"""
if name is None:
name = 'Char Code Field (' + str(columns) + ' columns)'
if columns <= 0:
raise BaseException()
char_sets = None
for char_set in _tables.get_data('character_set'):
regex = '[ ]{' + str(15 - len(char_set)) + '}' + char_set
if char_sets is None:
char_sets = regex
else:
char_sets += '|' + regex
# Accepted sets
_character_sets = pp.Regex(char_sets)
_unicode_1_16b = pp.Regex('U\+0[0-8,A-F]{3}[ ]{' + str(columns - 6) + '}')
_unicode_2_21b = pp.Regex('U\+0[0-8,A-F]{4}[ ]{' + str(columns - 7) + '}')
# Basic field
char_code_field = (_character_sets | _unicode_1_16b | _unicode_2_21b)
# Parse action
char_code_field = char_code_field.setParseAction(lambda s: s[0].strip())
# Name
char_code_field.setName(name)
return char_code_field
示例12: numeric
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def numeric(columns, name=None):
"""
Creates the grammar for a Numeric (N) field, accepting only the specified
number of characters.
This version only allows integers.
:param columns: number of columns for this field
:param name: name for the field
:return: grammar for the integer numeric field
"""
if name is None:
name = 'Numeric Field'
if columns <= 0:
# Can't be empty or have negative size
raise BaseException()
# Only numbers are accepted
field = pp.Regex('[0-9]{' + str(columns) + '}')
# Parse action
field.setParseAction(_to_int)
field.leaveWhitespace()
# Name
field.setName(name)
return field
示例13: date
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def date(name=None):
"""
Creates the grammar for a Date (D) field, accepting only numbers in a
certain pattern.
:param name: name for the field
:return: grammar for the date field
"""
if name is None:
name = 'Date Field'
# Basic field
# This regex allows values from 00000101 to 99991231
field = pp.Regex('[0-9][0-9][0-9][0-9](0[1-9]|1[0-2])'
'(0[1-9]|[1-2][0-9]|3[0-1])')
# Parse action
field.setParseAction(lambda d: datetime.datetime.strptime(d[0], '%Y%m%d')
.date())
# Name
field.setName(name)
# White spaces are not removed
field.leaveWhitespace()
return field
示例14: time
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def time(name=None):
"""
Creates the grammar for a Time or Duration (T) field, accepting only
numbers in a certain pattern.
:param name: name for the field
:return: grammar for the date field
"""
if name is None:
name = 'Time Field'
# Basic field
# This regex allows values from 000000 to 235959
field = pp.Regex('(0[0-9]|1[0-9]|2[0-3])[0-5][0-9][0-5][0-9]')
# Parse action
field.setParseAction(lambda t: datetime.datetime.strptime(t[0], '%H%M%S')
.time())
# White spaces are not removed
field.leaveWhitespace()
# Name
field.setName(name)
return field
示例15: ipi_base_number
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import Regex [as 别名]
def ipi_base_number(name=None):
"""
IPI Base Number field.
An IPI Base Number code written on a field follows the Pattern
C-NNNNNNNNN-M. This being:
- C: header, a character.
- N: numeric value.
- M: control digit.
So, for example, an IPI Base Number code field can contain I-000000229-7.
:param name: name for the field
:return: a parser for the IPI Base Number field
"""
if name is None:
name = 'IPI Base Number Field'
field = pp.Regex('I-[0-9]{9}-[0-9]')
# Name
field.setName(name)
field_num = basic.numeric(13)
field_num.setName(name)
field = field | field_num
# White spaces are not removed
field.leaveWhitespace()
return field.setResultsName('ipi_base_n')