简体   繁体   English

下划线前后的正则表达式匹配模式

[英]regex match patern before and after a underscore

I have a file name convention {referenceId}_{flavor name}.mp4 in kaltura. 我在kaltura中有一个文件名约定{referenceId} _ {flavor name} .mp4。 or if you are familiar with kaltura then tell me the slugRegex i could use for this naming convention that would support pre-encoded file ingestion 或者,如果您熟悉kaltura,请告诉我slugRegex,我可以使用该命名约定来支持预编码的文件提取

I have to extract referenceId and filename from it. 我必须从中提取referenceId和文件名。

I'm using 我正在使用

/(?P)_(?P)[.]\w{3,}/
var filename = "referenceId_flavor-name.mp4";
var parts = filename.match(/([^_]+)_([^.]+)\.(\w{3})/i);
// parts is an array with 4 elements
// ["referenceId_flavor-name.mp4", "referenceId", "flavor-name", "mp4];
var file = 'refID_name.mp4',
    parts = file.match(/^([^_]+)_(.+)\.mp4/, file);

Returns array: 返回数组:

[
    'refID_name.mp4', //the whole match is always match 0
    'refID', //sub-match 1
    'name' //sub-match 2
]
/**
 * Parse file name according to defined slugRegex and set the extracted parsedSlug and parsedFlavor.
 * The following expressions are currently recognized and used:
 *  - (?P<referenceId>\w+) - will be used as the drop folder file's parsed slug.
 *  - (?P<flavorName>\w+)  - will be used as the drop folder file's parsed flavor. 
 *  - (?P<userId>\[\w\@\.]+) - will be used as the drop folder file entry's parsed user id.
 * @return bool true if file name matches the slugRegex or false otherwise
 */
private function parseRegex(DropFolderContentFileHandlerConfig $fileHandlerConfig, $fileName, &$parsedSlug, &$parsedFlavor, &$parsedUserId)
{
    $matches = null;
    $slugRegex = $fileHandlerConfig->getSlugRegex();
    if(is_null($slugRegex) || empty($slugRegex))
    {
        $slugRegex = self::DEFAULT_SLUG_REGEX;
    }
    $matchFound = preg_match($slugRegex, $fileName, $matches);
    KalturaLog::debug('slug regex: ' . $slugRegex . ' file name:' . $fileName);
    if ($matchFound) 
    {
        $parsedSlug   = isset($matches[self::REFERENCE_ID_WILDCARD]) ? $matches[self::REFERENCE_ID_WILDCARD] : null;
        $parsedFlavor = isset($matches[self::FLAVOR_NAME_WILDCARD])  ? $matches[self::FLAVOR_NAME_WILDCARD]  : null;
        $parsedUserId = isset($matches[self::USER_ID_WILDCARD])  ? $matches[self::USER_ID_WILDCARD]  : null;
        KalturaLog::debug('Parsed slug ['.$parsedSlug.'], Parsed flavor ['.$parsedFlavor.'], parsed user id ['. $parsedUserId .']');
    }
    if(!$parsedSlug)
        $matchFound = false;
    return $matchFound;
} 

is the code that deals with the regex. 是处理正则表达式的代码。 I used /(?P<referenceId>.+)_(?P<flavorName>.+)[.]\\w{3,}/ and following this tutorial enter link description here 我使用了/(?P<referenceId>.+)_(?P<flavorName>.+)[.]\\w{3,}/并按照此教程在此处输入链接描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM