我的代码将我感兴趣的文件路径的两部分连接起来:
import glob
prefix = /aldo/programs/info
suffix = /final/*_cube/myFile.txt
prefix = prefix.rstrip()
file = glob.glob(prefix+'/final/*_cube/myFile.txt')
print (file)
打印最终文件给了我:
/aldo/programs/info/final/Michael_cube/myFile.txt
这是好的和有意的。但是,我试图将全局化的字符串设置为变量,在本例中,“Michael”。我尝试过使用正则表达式,但找不到一种方法来获取globbed的值(Michael)。我很困,任何指导都将不胜感激
DEMO
^.*?\/final\/(.*?)_cube\/myFile\.txt$
您可以从组1获取内容,或者用替换字符串
$1
替换整个匹配项以获得输出说明:
以
^
和$
开始和结束patren需要模式匹配整行。您可以用“match all”量词来解释数据中的任何未知数。.*?,然后您所需要做的就是用一个捕获组获取所需的输出您可以使用字符串切片,从结果中获得所有需要剥离的部分,以获得作为
*
-值提供的内容:输出:
相关问题 更多 >
编程相关推荐