用特定的起始字符串和结束字符串检查文件名

时间:2017-04-19 04:01:02

标签: regex perl

我需要使用perl来检查文件名是否与格式匹配。

JCL

我尝试过像

这样的正则表达式
Log4J, Logkit, JDK 1.4

但失败了。我怎么修改它?

3 个答案:

答案 0 :(得分:4)

你需要在\ d中添加一个量词,{8}只能连续匹配8位数。

[19/Apr/2017 03:48:03] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
File "/usr/lib64/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
File "/usr/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 153, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
    return callback(request, **param_dict)
File "/opt/graphite/webapp/graphite/views.py", line 7, in server_error
    template = loader.get_template(template_name)
File "/usr/lib/python2.7/site-packages/django/template/loader.py", line 145, in get_template
    template, origin = find_template(template_name)
File "/usr/lib/python2.7/site-packages/django/template/loader.py", line 138, in find_template
    raise TemplateDoesNotExist(name)
TemplateDoesNotExist: 500.html

有关正则表达式的详细信息,请参阅Perlre

答案 1 :(得分:2)

应该这样做

<强> \w{4}\d\_\w{2}\_\w{3}\_\d{8}[.csv|.CSV]+

<强> 演示

<强> https://regex101.com/r/JVKZYP/3

  

enter image description here

     

\w{4} 匹配任何字词(等于[a-zA-Z0-9_] {4} 正好匹配4次    \d 匹配数字(equal to [0-9]
   \_ 字面匹配字符 _ (区分大小写)
   \d{8} 匹配一个数字(equal to [0-9]{8}完全匹配8次
   [.csv|.CSV] 匹配列表.csv|CSV中的单个字符(区分大小写)

或修复你的[test1_ab_pls_]+\d{8}(.csv)

或其他匹配https://regex101.com/r/cAKUQN/1

<强> \w{4}\d\_\w{2}\_\w{3}\_(20\d{2})(\d{2})(\d{2})[.csv|.CSV]+

确切日期 ([2017]{4})([04]{2})([18]{2})

答案 2 :(得分:0)

它不漂亮,但这是另一种方式。

if ( index( $oldfile, 'test1_ab_pls_' ) == 0
    && rindex( $oldfile, '.csv' ) == length($oldfile) - 4 )
{ print "It matches!" }

我对它进行了基准测试,并且它比Fashim的正则表达式在正面匹配方面更快,但对于负面匹配则更慢。