假设有字符串
just/the/path/to/file.txt
我需要获取第一个和最后一个斜杠之间的部分:the/path/to
我想出了这个正则表达式:/^(.*?).([^\/]*)$/
,但是这给了我最后一个斜杠前面的所有内容。
最佳答案
不要使用 [^/]*
,因为它不会匹配任何包含斜杠的内容。只需使用 .*
来匹配任何内容:
/(.*?)\/(.*)\/(.*)/
第 1 组 = just
,第 2 组 = the/path/to
,第 3 组 = file.txt
。
https://stackoverflow.com/questions/69395139/