当前位置: 首页>>代码示例>>Python>>正文


Python Regex.parseString方法代码示例

本文整理汇总了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"],
   }
开发者ID:data-gov-ie,项目名称:cso2rdf,代码行数:14,代码来源:PCAxisParser.py

示例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
开发者ID:robinsiebler,项目名称:Tasklist_2x,代码行数:74,代码来源:tasks.py

示例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()
开发者ID:lnunno,项目名称:python,代码行数:5,代码来源:pyparsing_ex.py


注:本文中的pyparsing.Regex.parseString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。