简体   繁体   English

python3 glob.glob正则表达式仅获得第一个匹配项

[英]python3 glob.glob regex only getting first match

Having a bit of a weird issue, as this code seems to work perfectly well on my friends mac, but is not at all working on my ubuntu 16.04. 有一个奇怪的问题,因为这段代码在我的朋友mac上似乎可以很好地工作,但是在我的ubuntu 16.04上却根本无法工作。

Through my python, im running the following 通过我的python,我运行以下命令

filenames = glob.glob(opts['-I'])

which is attempting to match to a set of 32 text files with the format TEXT/text01.txt 试图匹配一组32个文本文件,格式为TEXT / text01.txt

My initial regex was the following 我最初的正则表达式如下

python -I TEXT/text??

Which returned zero files. 其中返回零个文件。

I have also tried 我也尝试过

python -I TEXT/text*

Which seems to only be returning text01.txt. 似乎只返回text01.txt。 Is there a corresponding regex that can get all of the text files instead of just the first one, and is there any reason why this is working on a mac but not ubuntu? 是否有相应的正则表达式可以获取所有文本文件,而不仅仅是第一个文件?是否有任何理由使它在Mac上有效但在ubuntu上无效?

the problem when you're calling 打电话时的问题

python -I TEXT/text*

is that TEXT/text* is expanded by the shell. TEXT/text*被shell扩展了。 So these are the exact args passed to python: 因此,这些是传递给python的确切参数:

-I TEXT/text01.txt TEXT/text02.txt (and other matching files)

argparser assigns text01.txt to the -I option and other arguments are ignored (check positional arguments to find them). argparser将text01.txt分配给-I选项,其他参数将被忽略(检查位置参数以查找它们)。 glob.glob returns the exact filename it recieves (note that text?? doesn't match text01.txt because you're missing the extension, or make it text??.txt ) glob.glob返回它glob.glob的确切文件名(请注意,由于缺少扩展名, text??text01.txt不匹配,或者将其text01.txt text??.txt

You need to quote your wildcard (not regex) 您需要引用通配符(而不是正则表达式)

python -I "TEXT/text*"

or escape the wildcard: 或转义通配符:

python -I TEXT/text\*

or use a more prehistoric command line like windows CMD where the wildcards are passed literally. 或者使用更史前的命令行(例如Windows CMD),通配符将按原样传递。

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

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