jia*_*amo 1 python regex shell path
ls
[cc]a.txt bba.txt [cc]b.txt bbb.txt
ipython
glob.glob("[cc]*")
[]
glob.glob("bb*")
['bba.txt','bbb.txt']
glob.glob("[bb]*")
['bba.txt','bbb.txt']
Run Code Online (Sandbox Code Playgroud)
如何获得[cc]a.txt
和[cc]b.txt
匹配?炭[
中[bb]*
似乎就没有任何意义.使用如下:
glob.glob("\[cc\]*")
[]
Run Code Online (Sandbox Code Playgroud)
也是NULL
封装[
和]
在[]
.
庆典:
test $ ls
[cc]a.txt test.dat test.gp test.py
Run Code Online (Sandbox Code Playgroud)
蟒蛇:
>>> glob.glob(r'[[]cc[]]*.txt') #yuck ...
['[cc]a.txt']
Run Code Online (Sandbox Code Playgroud)
在globbing方面,[ something]
表示匹配方括号之间的任何字符.所以[cc]*.txt
相当于c*.txt
哪个不是你想要的.
当然,更强大的解决方案是停止命名带有glob字符的文件; ^).
归档时间: |
|
查看次数: |
279 次 |
最近记录: |