本文整理汇总了Python中sre_parse.parse函数的典型用法代码示例。如果您正苦于以下问题:Python parse函数的具体用法?Python parse怎么用?Python parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test
def test():
import ClearMap.Utils.InverseRegularExpression as ire;
import sre_parse as sre;
reload(ire)
source = '/test/test_(?P<row>\d{4})_(?P<col>\d{3}).tif';
p = sre.parse(source);
ire.patternToExpression(p)
reload(ire)
source = r'/test/test_(?:\d)_(?P<col>\d{3})_[7-9][.](?=col)tif$';
p = sre.parse(source);
ire.patternToExpression(p)
开发者ID:ChristophKirst,项目名称:ClearMapUnstable,代码行数:13,代码来源:InverseRegularExpression.py
示例2: charclass_runner
def charclass_runner(pat):
r = Regex().get_parse_tree(pat)
regexlint_version = r.children[0].matching_character_codes
sre_parsed = sre_parse.parse(pat)
print(sre_parsed)
if isinstance(sre_parsed[0][1], int):
sre_chars = sre_parsed
else:
sre_chars = sre_parsed[0][1]
print('inner', sre_chars)
golden = list(expand_sre_in(sre_chars))
order_matters = True
try:
if (sre_parsed[0][0] == sre_constants.NOT_LITERAL or
sre_parsed[0][1][0][0] == sre_constants.NEGATE):
golden = [i for i in range(256) if i not in golden]
order_matters = False
except TypeError:
pass
print('sre_parse', golden)
print('regexlint', regexlint_version)
if order_matters:
assert golden == regexlint_version
else:
print('extra:', sorted(set(regexlint_version) - set(golden)))
print('missing:', sorted(set(golden) - set(regexlint_version)))
assert sorted(golden) == sorted(regexlint_version)
开发者ID:thatch,项目名称:regexlint,代码行数:30,代码来源:test_parser.py
示例3: reverse_group_map
def reverse_group_map(re_str):
r = re.compile(re_str)
ast = sre_parse.parse(re_str)
group_indices = r.groupindex
group_index_map = dict((index, group)
for (group, index) in r.groupindex.items())
return group_index_map
开发者ID:patricklaw,项目名称:surly,代码行数:7,代码来源:re_parse.py
示例4: compile
def compile(p, flags=0):
# internal: convert pattern list to internal format
if isstring(p):
pattern = p
p = sre_parse.parse(p, flags)
else:
pattern = None
code = _code(p, flags)
# print code
# XXX: <fl> get rid of this limitation!
if p.pattern.groups > 100:
raise AssertionError(
"sorry, but this version only supports 100 named groups"
)
# map in either direction
groupindex = p.pattern.groupdict
indexgroup = [None] * p.pattern.groups
for k, i in groupindex.items():
indexgroup[i] = k
return _sre.compile(
pattern, flags | p.pattern.flags, code,
p.pattern.groups - 1,
groupindex, indexgroup
)
开发者ID:ppyordanov,项目名称:HCI_4_Future_Cities,代码行数:30,代码来源:sre_compile.py
示例5: isFileExpression
def isFileExpression(source):
"""Checks if filename is a regular expression denoting a file list
Arguments:
source (str): source file name
Returns:
bool: True if source is true regular expression with at least one non-literal
Note:
The any character '.' is not treated as a non-literal because of possible filename extensions
"""
if not isinstance(source, basestring):
return False;
if isFile(source):
return False;
else:
#searchRegex = re.compile('.*\\\\d\{(?P<digit>\d)\}.*').search
#m = searchRegex(source);
#if m is None:
# return False;
#else:
# return True;
#parse regular expression
p = sre_parse.parse(source);
for l in p:
#note: allow for a filname.ext patterns although this is strictly a regular expression which should be denoted as filename\.ext
if l[0] != 'literal' and l[0] != 'any':
return True;
return False;
开发者ID:ChristophKirst,项目名称:ClearMapUnstable,代码行数:34,代码来源:IO.py
示例6: compile_regexp_to_noncapturing
def compile_regexp_to_noncapturing(pattern, flags=0):
"""
Convert all grouping parentheses in the given regexp pattern to
non-capturing groups, and return the result. E.g.:
>>> from nltk.internals import compile_regexp_to_noncapturing
>>> compile_regexp_to_noncapturing('ab(c(x+)(z*))?d')
'ab(?:c(?:x+)(?:z*))?d'
:type pattern: str
:rtype: str
"""
def convert_regexp_to_noncapturing_parsed(parsed_pattern):
res_data = []
for key, value in parsed_pattern.data:
if key == sre_constants.SUBPATTERN:
index, subpattern = value
value = (None, convert_regexp_to_noncapturing(subpattern))
elif key == sre_constants.GROUPREF:
raise ValueError('Regular expressions with back-references are not supported: {0}'.format(pattern))
res_data.append((key, value))
parsed_pattern.data = res_data
parsed_pattern.pattern.groups = 1
parsed_pattern.pattern.groupdict = {}
return parsed_pattern
return sre_compile.compile(convert_regexp_to_noncapturing_parsed(sre_parse.parse(pattern)))
开发者ID:osu-ling5802-2016,项目名称:Sigmorphon2016,代码行数:27,代码来源:internals.py
示例7: regex_score
def regex_score(regex, search_string):
"""
Returns a closeness score of how well the regex matches the string.
Will return -1 if it doesn't match.
"""
match = re.search(regex, search_string)
if match:
# Base score is the longest distance between regex, match,
# and search_string
regex_match_dist = levenshtein_distance(
regex.pattern.lower(), match.group(0).lower())
match_string_dist = levenshtein_distance(
match.group(0).lower(), search_string.lower())
score = max(regex_match_dist, match_string_dist)
# Adjust score: Special anchors slightly reduce distance
for opcode, argument in sre_parse.parse(regex.pattern):
if str(opcode) == 'AT':
if str(argument) == 'AT_BEGINNING' or 'AT_END':
# ^ or $, adjust 1 edit
score -= 1
if str(argument) == 'AT_BOUNDARY':
# all other anchors reduce 2 edits
score -= 2
return score if score >= 0 else 0
else:
return -1
开发者ID:KonstantinKo,项目名称:marvin,代码行数:32,代码来源:regex_distance.py
示例8: re_replace_literals
def re_replace_literals(text, mapping):
"""Raises NotImplementedError or re.error"""
assert isinstance(text, unicode)
pattern = sre_parse.parse(text)
return _construct_regexp(pattern, mapping)
开发者ID:urielz,项目名称:quodlibet,代码行数:7,代码来源:_diacritic.py
示例9: __init__
def __init__(self, lexicon, flags=FLAGS):
self.actions = [None]
# combine phrases into a compound pattern
s = sre_parse.Pattern()
s.flags = flags
p = []
# NOTE(kgibbs): These lines must be added to make this file work under
# Python 2.2, which is commonly used at Google.
def enumerate(obj):
i = -1
for item in obj:
i += 1
yield i, item
# NOTE(kgibbs): End changes.
for idx, token in enumerate(lexicon):
phrase = token.pattern
try:
subpattern = sre_parse.SubPattern(s,
[(SUBPATTERN, (idx + 1, sre_parse.parse(phrase, flags)))])
except sre_constants.error:
raise
p.append(subpattern)
self.actions.append(token)
s.groups = len(p)+1 # NOTE(guido): Added to make SRE validation work
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
self.scanner = sre_compile.compile(p)
开发者ID:1stvamp,项目名称:brumcon9-talk,代码行数:27,代码来源:scanner.py
示例10: clean_pattern
def clean_pattern(pattern):
"""
Cleans URL patterns
* pattern => token
* '2' => ('literal', 50)
* '2|3' => ('in', [('literal', 50), ('literal', 51)])
"""
star = '*'
parsed = sre_parse.parse(pattern)
literals = []
for token in parsed:
if token[0] == LITERAL:
character = quote(unichr(token[1]).encode('utf8'))
literals.append(character)
elif token[0] == AT:
pass
elif literals[-1:] != [star]:
literals.append(star)
rule = '/' + ''.join(literals)
if parsed and not rule.endswith(star):
if parsed[-1] == (AT, AT_END):
rule += '$'
else:
rule += star
return rule
开发者ID:dimka665,项目名称:django-url-robots,代码行数:30,代码来源:utils.py
示例11: compile
def compile(p, flags=0):
# internal: convert pattern list to internal format
if isstring(p):
pattern = p
p = sre_parse.parse(p, flags)
else:
pattern = None
code = _code(p, flags)
if flags & SRE_FLAG_DEBUG:
print()
dis(code)
# map in either direction
groupindex = p.state.groupdict
indexgroup = [None] * p.state.groups
for k, i in groupindex.items():
indexgroup[i] = k
return _sre.compile(
pattern, flags | p.state.flags, code,
p.state.groups-1,
groupindex, tuple(indexgroup)
)
开发者ID:Eyepea,项目名称:cpython,代码行数:26,代码来源:sre_compile.py
示例12: charclass_runner
def charclass_runner(pat):
r = Regex().get_parse_tree(pat)
regexlint_version = r.children[0].matching_character_codes
sre_parsed = sre_parse.parse(pat)
print sre_parsed
if isinstance(sre_parsed[0][1], int):
sre_chars = sre_parsed
else:
sre_chars = sre_parsed[0][1]
golden = list(expand_sre_in(sre_chars))
order_matters = True
try:
if (sre_parsed[0][0] == 'not_literal' or
sre_parsed[0][1][0][0] == 'negate'):
golden = [i for i in range(256) if i not in golden]
order_matters = False
except TypeError:
pass
print golden
print regexlint_version
if order_matters:
assert golden == regexlint_version
else:
assert sorted(golden) == sorted(regexlint_version)
开发者ID:birkenfeld,项目名称:regexlint,代码行数:25,代码来源:test_parser.py
示例13: compile
def compile(p, flags=0):
# internal: convert pattern list to internal format
if type(p) in STRING_TYPES:
import sre_parse
pattern = p
p = sre_parse.parse(p, flags)
else:
pattern = None
code = _code(p, flags)
# print code
# FIXME: <fl> get rid of this limitation!
assert p.pattern.groups <= 100,\
"sorry, but this version only supports 100 named groups"
# map in either direction
groupindex = p.pattern.groupdict
indexgroup = [None] * p.pattern.groups
for k, i in groupindex.items():
indexgroup[i] = k
return _sre.compile(
pattern, flags, code,
p.pattern.groups-1,
groupindex, indexgroup
)
开发者ID:asottile,项目名称:ancient-pythons,代码行数:29,代码来源:sre_compile.py
示例14: doReversing
def doReversing(p):
# p = 'ab*de.gh+i{10}'
# p = re.compile()
dbg("Pattern:" + p)
pattern = sre_parse.parse(p, 0)
out = reverse(pattern)
return out
开发者ID:Jitendrakry,项目名称:rule2alert,代码行数:7,代码来源:RevRegex2.py
示例15: regex_slice
def regex_slice(expr, start, end):
"""
Get a slice of a regex by calling regex_index on each index.
Note that this can return expressions that are overly general: for example,
it can mix characters from both branches of a regex. Being more specific
than that would take more work.
>>> regex_slice('test', 0, 1)
't'
>>> regex_slice('t?est', 0, 2)
'[te][es]'
>>> regex_slice('mo+', 3, 8)
'ooooo'
"""
if start < 0 or end < 0:
raise NotImplementedError("Can't take negative slices of a regex yet")
result = ''
for index in range(start, end):
choices = _regex_index_pattern(parse(expr), index)
if len(choices) == 0:
return None
elif len(choices) == 1:
regex = unparse(choices[0])
result += regex
else:
regex = round_trip(unparse(('branch', (None, choices))))
if '|' in regex:
result += '(%s)' % (regex,)
else:
result += regex
return result
开发者ID:rspeer,项目名称:solvertools-2014,代码行数:33,代码来源:regextools.py
示例16: __init__
def __init__(self, lexicons, init_state=None, flags=0):
# All the regexp magic below is copied from re.Scanner from
# the standard library.
import sre_compile
import sre_parse
from sre_constants import BRANCH, SUBPATTERN
if init_state is None:
init_state = State()
if not hasattr(init_state, 'start'):
init_state.start = None
self.init_state = init_state
self.lexicons = lexicons
self.scanners = {}
for start, lexicon in lexicons.iteritems():
# combine phrases into a compound pattern
p, a = [], []
s = sre_parse.Pattern()
s.flags = flags
for phrase, action in lexicon:
p.append(sre_parse.SubPattern(s, [
(SUBPATTERN, (len(p)+1,
sre_parse.parse(phrase, flags))),
]))
a.append(action)
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
s.groups = len(p)
self.scanners[start] = sre_compile.compile(p).match, a
开发者ID:Serabe,项目名称:geogebra,代码行数:27,代码来源:lexing.py
示例17: __init__
def __init__(self, pattern, flags=0):
self.pattern = pattern
try:
self.parsed_pattern = sre_parse.parse(pattern, flags)
except error, e:
raise ErrorUnparseable(
'Invalid regex %s failed: %s' % (pattern,e.message))
开发者ID:jdukes,项目名称:raz,代码行数:7,代码来源:raz.py
示例18: base_regex_strategy
def base_regex_strategy(regex, parsed=None):
if parsed is None:
parsed = sre.parse(regex.pattern)
return clear_cache_after_draw(_strategy(
parsed,
Context(flags=regex.flags),
regex.pattern
))
开发者ID:doismellburning,项目名称:hypothesis,代码行数:8,代码来源:regex.py
示例19: ipermute
def ipermute(p):
r"""Generate permutations (returns an iterable rather than an array or list)
>>> list(ipermute(r'[A-Z]\d'))
['A0', 'B0', ..., 'Z9']
"""
toks = [tok_n_val for tok_n_val in sre_parse.parse(p)]
return permute_toks(toks)
开发者ID:totalgood,项目名称:guten,代码行数:8,代码来源:er.py
示例20: base_regex_strategy
def base_regex_strategy(regex, parsed=None):
if parsed is None:
parsed = sre_parse.parse(regex.pattern, flags=regex.flags)
return clear_cache_after_draw(
_strategy(
parsed, Context(flags=regex.flags), isinstance(regex.pattern, text_type)
)
)
开发者ID:HypothesisWorks,项目名称:hypothesis-python,代码行数:8,代码来源:regex.py
注:本文中的sre_parse.parse函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论