du1462 2013-03-28 15:21
浏览 19
已采纳

限制文件名匹配以特定方式启动

I have thousands of files in a directory that i am running a batch script on to process.

The relevant files all start with XX-XX-XX (at least one integer, but no more than two, with underscores separating the three numbers)

I used txt2re to create this:

 $txt='1-2-3';

  $re1='(\\d+)';    # Integer Number 1
  $re2='(-)';   # Any Single Character 1
  $re3='(\\d+)';    # Integer Number 2
  $re4='(-)';   # Any Single Character 2
  $re5='(\\d+)';    # Integer Number 3

  if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5."/is", $txt, $matches))
  {
      $int1=$matches[1][0];
      $c1=$matches[2][0];
      $int2=$matches[3][0];
      $c2=$matches[4][0];
      $int3=$matches[5][0];
      print "($int1) ($c1) ($int2) ($c2) ($int3) 
";
  }

Is it possible to get a slightly more compact version of this, that I can compare to the filename in a single function.

  • 写回答

2条回答 默认 最新

  • dqm74406 2013-03-28 15:39
    关注

    You could use

    "/^\d\d?-\d\d?-\d\d?/"
    

    If the digits are separated by underscores use _ instead of -.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭