MCPcopy
hub / github.com/yt-dlp/yt-dlp / _search_regex

Method _search_regex

yt_dlp/extractor/common.py:1315–1348  ·  view source on GitHub ↗

Perform a regex search on the given string, using a single or a list of patterns returning the first matching group. In case of failure return a default value or raise a WARNING or a RegexNotFoundError, depending on fatal, specifying the field name.

(self, pattern, string, name, default=NO_DEFAULT, fatal=True, flags=0, group=None)

Source from the content-addressed store, hash-verified

1313 }
1314
1315 def _search_regex(self, pattern, string, name, default=NO_DEFAULT, fatal=True, flags=0, group=None):
1316 """
1317 Perform a regex search on the given string, using a single or a list of
1318 patterns returning the first matching group.
1319 In case of failure return a default value or raise a WARNING or a
1320 RegexNotFoundError, depending on fatal, specifying the field name.
1321 """
1322 if string is None:
1323 mobj = None
1324 elif isinstance(pattern, (str, re.Pattern)):
1325 mobj = re.search(pattern, string, flags)
1326 else:
1327 for p in pattern:
1328 mobj = re.search(p, string, flags)
1329 if mobj:
1330 break
1331
1332 _name = self._downloader._format_err(name, self._downloader.Styles.EMPHASIS)
1333
1334 if mobj:
1335 if group is None:
1336 # return the first matching group
1337 return next(g for g in mobj.groups() if g is not None)
1338 elif isinstance(group, (list, tuple)):
1339 return tuple(mobj.group(g) for g in group)
1340 else:
1341 return mobj.group(group)
1342 elif default is not NO_DEFAULT:
1343 return default
1344 elif fatal:
1345 raise RegexNotFoundError(f'Unable to extract {_name}')
1346 else:
1347 self.report_warning(f'unable to extract {_name}' + bug_reports_message())
1348 return None
1349
1350 def _search_json(self, start_pattern, string, name, video_id, *, end_pattern='',
1351 contains_pattern=r'{(?s:.+)}', fatal=True, default=NO_DEFAULT, **kwargs):

Callers 15

_search_jsonMethod · 0.95
_html_search_regexMethod · 0.95
_og_search_propertyMethod · 0.95
_search_nuxt_dataMethod · 0.95
_form_hidden_inputsMethod · 0.95
_parse_smil_namespaceMethod · 0.95
_parse_mpd_periodsMethod · 0.95
_real_extractMethod · 0.80
_real_extractMethod · 0.80
_real_extractMethod · 0.80
extract_dataMethod · 0.80
_real_extractMethod · 0.80

Calls 5

report_warningMethod · 0.95
RegexNotFoundErrorClass · 0.85
bug_reports_messageFunction · 0.85
_format_errMethod · 0.80
groupsMethod · 0.80

Tested by

no test coverage detected