0

这是示例

SF_Library/example/Platform/Analyses-PLATFORM.part0.xml
SF_Library/example/Platform/Models-PLATFORM.part0.xml
SF_Library/example/Platform/Models-PLATFORM.car
SF_Library/example/Platform/DS-PLATFORM.car

我想抓住下面的基本路径。

SF_Library/example/Platform/

有人知道我应该使用什么正则表达式吗?

4

4 回答 4

7

您不需要正则表达式:

#!/bin/bash

fullpath="SF_Library/example/Platform/Analyses-PLATFORM.part0.xml"
# or if you read them then: while read fullpath; do

basename=${fullpath%/*}

# or if you read them then: done < input_file.txt
于 2012-11-14T20:42:15.973 回答
3

正则表达式不适用于提取子字符串。为什么不使用dirname命令?

$ dirname /home/foo/whatever.txt
/home/foo
$

如果您在变量中需要它:

DIRECTORY=`basename "SF_Library/example/Platform/DS-PLATFORM.car"`
于 2012-11-14T20:42:33.850 回答
2

您可以使用dirname命令:

dirname SF_Library/example/Platform/DS-PLATFORM.car

它会给你:SF_Library/example/Platform

于 2012-11-14T20:42:31.710 回答
1

好吧,我就纵容你。

^(.*/).*$

解剖:

^     beginning of string
(     start of capture group
  .*  series of any number of any character
  /   a slash
)     end of capture group
.*    series of any number of characters that are not slashes
$     end of string

这是因为*贪心:它匹配尽可能多的字符(因此它将包括所有斜杠直到最后一个)。

但正如其他答案所指出的那样,正则表达式可能不是最好的方法。

于 2012-11-14T20:43:14.847 回答