当前位置: 动力学知识库 > 问答 > 编程问答 >

Regex: Trying to match filenames with 6 digit integers

问题描述:

I am attempting to use the find command with a regex to match filenames which contain a 6 digit integer.

This is the command I am running:

EVENT_GENERATOR="Se82.2nubb"

find ./ -type f -regex "^./flmyptd2root-$EVENT_GENERATOR-[0-9]{6}.root$"

And here are the filenames I expect to match:

./flmyptd2root-Se82.2nubb-000005.root

./flmyptd2root-Se82.2nubb-000001.root

./flmyptd2root-Se82.2nubb-000004.root

./flmyptd2root-Se82.2nubb-000000.root

./flmyptd2root-Se82.2nubb-000003.root

./flmyptd2root-Se82.2nubb-100000.root

./flmyptd2root-Se82.2nubb-000002.root

It works if I do this instead:

find ./ -type f -regex "^./flmyptd2root-$EVENT_GENERATOR-[0-9]*.root$"

But I don't know why that works when the first expression does not. (* repeats the match [0-9] an arbitrary number of times I believe, so I guess this selects out all possible integers?)

How can I get my expression to select only 6 digit integers?

网友答案:

You can use this find command:

find . -regextype posix-extended  -type f -regex "^\./flmyptd2root-$EVENT_GENERATOR-[0-9]{6}\.root$"

find uses BRE by default where { and } are taken literally, use -regextype posix-extended as suggested above to correctly interpret range quantifier {...}.

分享给朋友:
您可能感兴趣的文章:
随机阅读: