简体   繁体   English

PHP preg_match和正则表达式

[英]PHP preg_match and regular expressions

I am fairly new to PHP and regular expressions, after reading i have got this far trying to understand how to extract correct info, 我对PHP和正则表达式相当陌生,在阅读完本文之后,我已经尽了最大努力来了解如何提取正确的信息,

Sample data 样本数据

2011/09/20  00:57       367,044,608 S1E04 - Cancer Man.avi
2012/03/12  03:01       366,991,496 Family Guy - S09E01 - And Then There Were Fewer.avi
2012/03/25  00:27        53,560,510 Avatar- The Legend of Korra S01E01.avi

What i would like to extract is the Date, File size and name of the file, remembering that the file can start with basically anything. 我想提取的是日期,文件大小和文件名,请记住该文件可以以任何东西开头。 and file size changes all the time. 文件大小一直在变化。

What i have currently. 我目前有什么。

$dateModifyed = substr($file, 0, 10); 
$fileSize = preg_match('[0-9]*/[0-9]*/[0-9]*/s[0-9]*:[0-9]*/s*', $file, $match)
$FileName = 

Full code i am working on 我正在处理的完整代码

function recursivePrint($folder, $subFolders, $Jsoncounter) {
$f = fopen("file.json", "a");

echo '{ "id" : "' . $GLOBALS['Jsoncounter'] . '", parent" : "' . "#" . '", Text" : "' . $folder . '" },' . "\n";
$PrintString = '{ "id" : "' . $GLOBALS['Jsoncounter'] . '", parent" : "' . "#" . '", Text" : "' . $folder . '" },' . "\n";
fwrite($f, $PrintString);
$foldercount = $GLOBALS['Jsoncounter'];
$GLOBALS['Jsoncounter']++;
foreach($subFolders->files as $file) {


    preg_match('/^(\d{4}/\d{2}/\d{2}\s+\d{2}:\d{2})\s+([\d,]+)\s+(.*)$/', $file, $match);
    $dateModified = $match[1];
    $fileSize = str_replace(',', '', $match[2]);
    $fileName = $match[3];
    echo $dateModified . $fileSize . $fileName;


    echo '{ "id" : "' . $GLOBALS['Jsoncounter'] . '", parent" : "' . $foldercount . '", Text" : "' . $file . '" },';
    $PrintString ='{ "id" : "' . $GLOBALS['Jsoncounter'] . '", parent" : "' . $foldercount . '", Text" : "' . $file . '" },';
    fwrite($f, $PrintString);
    $GLOBALS['Jsoncounter']++;
}

foreach($subFolders->folders as $folder => $subSubFolders) {
    recursivePrint($folder, $subSubFolders, $Jsoncounter);
}
fclose($f); 

} }

Any help extracting the correct numbers would be greatly appreciated 提取正确数字的任何帮助将不胜感激

There are several problems in your regex: 正则表达式中存在几个问题:

preg_match('[0-9]*/[0-9]*/[0-9]*/s[0-9]*:[0-9]*/s*', $file, $match)
            ^--missing delimiter ^            ^-- asterisk instead of plus
                                 |--literal s instead of \s

and of course you haven't used anchors or capturing groups , and the regex isn't finished yet. 当然,您还没有使用锚点捕获组 ,并且正则表达式尚未完成。

Try the following: 请尝试以下操作:

preg_match_all(
    '%^                     # Start of line
    ([0-9]+/[0-9]+/[0-9]+)  # Date (group 1)
    \s+                     # Whitespace
    ([0-9]+:[0-9]+)         # Time (group 2)
    \s+                     # Whitespace
    ([0-9,]+)               # File size (group 3)
    \s+                     # Whitespace
    (.*)                    # Rest of the line%mx', 
    $file, $result, PREG_SET_ORDER);
for ($matchi = 0; $matchi < count($result); $matchi++) {
    for ($backrefi = 0; $backrefi < count($result[$matchi]); $backrefi++) {
        # Matched text = $result[$matchi][$backrefi];

so for example $result[0][1] will contain 2011/09/20 , and $result[2][4] will contain Avatar- The Legend of Korra S01E01.avi etc. 因此,例如$result[0][1]将包含2011/09/20 ,而$result[2][4]将包含Avatar- The Legend of Korra S01E01.avi等。

You need to use capture groups to get the parts of the string that are matched by different parts of the regular expression. 您需要使用捕获组来获取与正则表达式的不同部分匹配的字符串部分。 Capture groups use parentheses around portions of the regexp. 捕获组在正则表达式的部分周围使用括号。

preg_match('#^(\d{4}/\d{2}/\d{2}\s+\d{2}:\d{2})\s+([\d,]+)\s+(.*)$#', $string, $match);
$dateModified = $match[1];
$fileSize = str_replace(',', '', $match[2]);
$fileName = $match[3];

Other problems in your regexp: 您的正则表达式中的其他问题:

  • You left out the delimiters at the beginning and end. 您在开头和结尾处省略了定界符。
  • You used /s instead of \\s for whitespace characters. 您将/s而不是\\s用于空格字符。

There's a tutorial on regular expressions at www.regular-expressions.info . www.regular-expressions.info上有一个有关正则表达式的教程。

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

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