本文整理汇总了Python中pyparsing.Regex.parseString方法的典型用法代码示例。如果您正苦于以下问题:Python Regex.parseString方法的具体用法?Python Regex.parseString怎么用?Python Regex.parseString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyparsing.Regex
的用法示例。
在下文中一共展示了Regex.parseString方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parseDate
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import parseString [as 别名]
def parseDate(self, dateString):
dateGrammar = Regex("\d{4}")("year") + Regex("\d{2}")("month") +\
Regex("\d{2}")("day") + Regex("\d{2}")("hours") +\
Suppress(":") + Regex("\d{2}")("minutes")
results = dateGrammar.parseString(dateString)
return {
"year" : results["year"],
"month" : results["month"],
"day" : results["day"],
"hours" : results["hours"],
"minutes" : results["minutes"],
}
示例2: validate_args
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import parseString [as 别名]
def validate_args(docopt_args):
"""Validate the user provided information."""
# validate priority
if docopt_args['-p']:
docopt_args['-p'] = docopt_args['-p'].upper()
if docopt_args['-p'] not in ['L', 'M', 'H']:
print '\nThe priority given is not valid.\n'
sys.exit(-4)
# validate date
if docopt_args['-d']:
pattern = Regex('^(1[0-2]|0?[1-9])(?P<Sep>(/|-|.))(3[01]|[12][0-9]|0?[1-9])(?P=Sep)(?:[0-9]{2})?[0-9]{2}$')
try:
pattern.parseString(docopt_args['-d'])
except ParseException:
print '\n' + docopt_args['-d'] + ' is not a valid date\n'
sys.exit(-3)
else:
# figure out what format the user provided...
if '/' in docopt_args['-d']:
date_sep = '/'
elif '-' in docopt_args['-d']:
date_sep = '-'
elif '.' in docopt_args['-d']:
date_sep = '.'
date = docopt_args['-d'].split(date_sep)
if len(date[0]) == 1:
month_format = 'M'
else:
month_format = 'MM'
if len(date[1]) == 1:
day_format = 'D'
else:
day_format = 'DD'
if len(date[2]) == 2:
year_format = 'YY'
else:
year_format = 'YYYY'
# get the time zone
# offset = time.strftime('%z', time.localtime()) <--- This doesn't work on windows
offset = ("-" if time.timezone > 0 else "+") + time.strftime("%H:%M", time.gmtime(abs(time.timezone)))
# parse the date
date_format = date_sep.join([month_format, day_format, year_format])
date_format = date_format + ' h:mm A Z'
if '--time' in docopt_args and docopt_args['--time']:
pattern = Regex('^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$')
try:
pattern.parseString(docopt_args['--time'])
except ParseException:
print '\nInvalid time format. Dropping Due Time.\n'
else:
# parse the date to make sure it is in the correct format:
dtime = docopt_args['--time'][:-2].strip()
period = docopt_args['--time'][-2:]
dtime = dtime + ' ' + period + ' ' + str(offset)
date = docopt_args['-d'] + ' ' + dtime
else:
date = docopt_args['-d'] + ' 11:59 PM ' + offset
date = arrow.get(date, date_format)
# validate due date occurs in the future
if date.date() < date.date().today():
print '\nWarning: The due date provided occurs in the past.\n'
docopt_args['-d'] = [date.format(date_format), date_format]
return docopt_args
示例3: email_address_parser
# 需要导入模块: from pyparsing import Regex [as 别名]
# 或者: from pyparsing.Regex import parseString [as 别名]
def email_address_parser(text):
emailExpr = Regex(r"(?P<user>[A-Za-z0-9._%+-]+)@(?P<hostname>[A-Za-z0-9.-]+)\.(?P<domain>[A-Za-z]{2,4})")
print emailExpr.parseString(text).dump()