本文整理匯總了Python中regex.error方法的典型用法代碼示例。如果您正苦於以下問題:Python regex.error方法的具體用法?Python regex.error怎麽用?Python regex.error使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類regex
的用法示例。
在下文中一共展示了regex.error方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: gather_relevant_pids
# 需要導入模塊: import regex [as 別名]
# 或者: from regex import error [as 別名]
def gather_relevant_pids():
url_pids = set()
for result in sparql_queries.url_pids():
url_pids.add(result)
ext_id_pids_to_urls = defaultdict(dict)
for result in sparql_queries.external_id_pids_and_urls():
for pid, formatters in result.items():
for formatter_url, formatter_regex in formatters.items():
if formatter_regex:
try:
compiled_regex = re.compile(formatter_regex)
except re.error:
LOGGER.debug(
"Using 'regex' third-party library. Formatter regex not supported by the 're' standard library: %s",
formatter_regex,
)
try:
compiled_regex = regex.compile(formatter_regex)
except regex.error:
LOGGER.debug(
"Giving up. Formatter regex not supported by 'regex': %s",
formatter_regex,
)
compiled_regex = None
else:
compiled_regex = None
ext_id_pids_to_urls[pid][formatter_url] = compiled_regex
return url_pids, ext_id_pids_to_urls
示例2: test_refang_never_excepts_from_urlparse
# 需要導入模塊: import regex [as 別名]
# 或者: from regex import error [as 別名]
def test_refang_never_excepts_from_urlparse(self):
try:
iocextract.refang_url('hxxp__test]')
iocextract.refang_url('CDATA[^h00ps://test.com/]]>')
except ValueError as e:
self.fail('Unhandled parsing error in refang: {e}'.format(e=e))
示例3: test_extract_custom_iocs_excepts_on_bad_regex
# 需要導入模塊: import regex [as 別名]
# 或者: from regex import error [as 別名]
def test_extract_custom_iocs_excepts_on_bad_regex(self):
# Note: have to use list() here because exceptions are only raised when
# the generator is executed.
with self.assertRaises(re.error):
list(iocextract.extract_custom_iocs('', [r'(mismatched paren']))
list(iocextract.extract_custom_iocs('', [r'[mismatched bracket']))
with self.assertRaises(IndexError):
list(iocextract.extract_custom_iocs('', [r'no capture group']))
list(iocextract.extract_custom_iocs('', [r'']))
示例4: error
# 需要導入模塊: import regex [as 別名]
# 或者: from regex import error [as 別名]
def error(message):
'''log error message, see the :mod:`logging` module'''
logging.error(message)
raise ValueError("UMI-tools failed with an error. Check the log file")
示例5: __init__
# 需要導入模塊: import regex [as 別名]
# 或者: from regex import error [as 別名]
def __init__(self, lang):
self._whitelist = set()
regexes = []
lang = lang.lower().replace('_', '-')
while True:
path = os.path.join(datadir, lang)
try:
file = open(path, 'rt', encoding='UTF-8')
except FileNotFoundError:
[lang, *suffix] = lang.rsplit('-', 1)
if suffix:
continue
else:
break
macros = Macros()
n = None # hi, pylint
def error(reason): # no coverage
return SyntaxError(reason, (file.name, n, 1, whole_line))
with file:
for n, line in enumerate(file, 1):
whole_line = line
if line.startswith('#'):
continue
line = line.split()
if not line:
continue
if line[0] == '*':
[word] = line[1:]
self._whitelist.add(word)
self._whitelist.add(word.upper())
self._whitelist.add(word.title())
elif line[0][0] == '@':
if (len(line) >= 4) and (line[0] == '@define') and (line[2] == '='):
(_, name, _, *definition) = line
definition = r'(?:{re})'.format(re=r'\s+'.join(definition))
try:
re.compile(definition)
except re.error as exc: # no coverage
raise error(exc)
try:
macros[name] = macros.expand(definition) # pylint: disable=unsubscriptable-object
except KeyError: # no coverage
raise error('duplicate macro definition: {}'.format(name))
else:
raise error('malformed @-command') # no coverage
else:
regex = r'\s+'.join(line)
regex = macros.expand(regex)
try:
re.compile(regex)
except re.error as exc: # no coverage
raise error(exc)
regexes += [regex]
break
if regexes:
regex = r'\b(?:(?i){0})\b'.format(
'|'.join(regexes)
)
self._find = re.compile(regex).finditer
else:
self._find = _find_nothing
示例6: extract_custom_iocs
# 需要導入模塊: import regex [as 別名]
# 或者: from regex import error [as 別名]
def extract_custom_iocs(data, regex_list):
"""Extract using custom regex strings.
Will always yield only the first *group* match from each regex.
Always use a single capture group! Do this::
[
r'(my regex)', # This yields 'my regex' if the pattern matches.
r'my (re)gex', # This yields 're' if the pattern matches.
]
NOT this::
[
r'my regex', # BAD! This doesn't yield anything.
r'(my) (re)gex', # BAD! This yields 'my' if the pattern matches.
]
For complicated regexes, you can combine capture and non-capture groups,
like this::
[
r'(?:my|your) (re)gex', # This yields 're' if the pattern matches.
]
Note the (?: ) syntax for noncapture groups vs the ( ) syntax for the capture
group.
:param data: Input text
:param regex_list: List of strings to treat as regex and match against data.
:rtype: Iterator[:class:`str`]
"""
# Compile all the regex strings first, so we can error out quickly.
regex_objects = []
for regex_string in regex_list:
regex_objects.append(re.compile(regex_string))
# Iterate over regex objects, running each against input data.
for regex_object in regex_objects:
for ioc in regex_object.finditer(data):
yield ioc.group(1)