使用正则表达式排除文件夹并匹配根文件夹中的所有.html模式文件

时间:2013-11-26 07:56:36

标签: regex drupal migration

我正在从html迁移到Drupal。使用迁移模块。

在我们的自定义迁移脚本中,我需要匹配除images文件夹以外的所有文件夹中的所有.html文件。

将此正则表达式传递给$list_files = new MigrateListFiles([],[],$regex)

以下是html文件的格式

/magazines/sample.html 
/test/index.html
/test/format_ss1.html
/test/folder/newstyle_1.html
/images/two.html

我只需要获得前2个html文件,即我们要排除以图像文件夹中的“_ [0-9]”和“_ss [0-9]”以及.hmtl文件结尾的文件。

我已成功完成排除3和4但我无法在图像文件夹中排除.html文件。

$regex = '/[a-zA-Z0-9\-][^_ss\d][^_\d]+\.html/'; //this will do for 3 and 4 files 

但我需要排除图片文件夹..

我试过像

$regex = '/[^images\/][a-zA-Z0-9\-][^_ss\d][^_\d]+\.html/'; // not working

在PHP脚本中,它将起作用

$regex = '~^(?!/images/)[a-zA-Z0-9/-]+(?!_ss\d|\d)\.html$~' //works in php script

有人可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

尝试 /((?!images)[0-9a-zA-Z])+/[^_]*[^\d]+\.html

匹配

/magazines/sample.html 
/test/index.html
/test/folder/newstyle.html
/test/format_ss.html

不匹配:

/test/format_ss1.html
/test/folder/newstyle_1.html
/images/two.html
/images/1.html
/test/folder/newstyle1.html
/test/folder/newstyle_12.html

这是可以接受的吗?

答案 1 :(得分:0)

这是一个Drupal / Migrate特定问题 - 正则表达式只是文件名(而不是目录)的正则表达式,因为它最终传递给https://api.drupal.org/api/drupal/includes%21file.inc/function/file_scan_directory/7

  

file_scan_directory($ dir,$ mask,$ options = array(),$ depth = 0)

     

$ mask:要查找的文件的preg_match()正则表达式。

我认为排除某些目录的唯一方法是在prepareRow()函数中抛出false,如果该行有一个你不需要的路径。

  

function prepareRow($ row)   在加载数据行之后,源类next()方法调用prepareRow()方法。参数$ row是一个stdClass对象,包含源提供的原始数据。实现prepareRow()有两个主要原因:

     

在通过任何其他方法和处理程序之前修改数据行:例如,获取相关数据,拆分源字段,根据某些逻辑组合或创建新的源字段。

     

有条件地跳过一行(通过返回FALSE)。

https://www.drupal.org/node/1132582