如何使用 PERL、正则表达式仅将文件名(非完整路径)放入 $1
我只想保留文件名(而不是完整路径)并将文件名添加到某些 bbcode 中。
这是要转换的 HTML:
<a href=/path/to/full/image.jpg rel=prettyPhoto><img rel=prettyPhoto src=/path/to/thumb/image.jpg /></a>
注意,我不能有 rel="foo" (没有双引号)..
这是我在 PERL 中执行转换的内容:
s/\<a href=(.+?)\ rel=prettyPhoto\>\<img rel=prettyPhoto src=(.+?) \/>\<\/a\>/\[box\]$1\[\/box\]/gi;
这会将 HTML 转换为:
[box]/path/to/image.jpg[/box]
但这就是我想要的结果:
[box]image.jpg[/box]
HTML 必须保持不变。那么如何更改 PERL 以使 $1 只包含文件名呢?
I want to keep only the filenames (not full paths) and add the filename to some bbcode.
Here is the HTML to be converted:
<a href=/path/to/full/image.jpg rel=prettyPhoto><img rel=prettyPhoto src=/path/to/thumb/image.jpg /></a>
Notice I cannot have rel="foo" (no double quotes)..
Here is what I have in PERL, to perform the conversion:
s/\<a href=(.+?)\ rel=prettyPhoto\>\<img rel=prettyPhoto src=(.+?) \/>\<\/a\>/\[box\]$1\[\/box\]/gi;
This converts the HTML to:
[box]/path/to/image.jpg[/box]
But this is what I want as a result:
[box]image.jpg[/box]
The HTML must remain the same. So how do I change my PERL so that $1 contains only the filename?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
(?:.*\/)?
将匹配以 / 结尾的最长部分。最后的
?
使其成为可选的。(?:.*\/)?
Will match the longest part finishing by a /. The final
?
makes this optional.我不知道它是否可以处理边缘情况,但我让它工作:
但是,您不想做类似的事情吗:
使用 HTML 解析器(如
HTML::TokeParser
,文档中有示例)为您找到网址?比手动调整 HTML 好得多。I don't know if it handles fringe cases, but I got this to work:
However, wouldn't you rather do something like:
using an HTML parser (like
HTML::TokeParser
, which has examples in the documentation) to find the url for you? Much better than relying on regexing the HTML by hand.我建议您使用正确的工具来完成这项工作,例如:
朋友不允许朋友使用正则表达式解析 HTML。
I suggest you use the right tools for the job, like these:
Friends don't let friends parse HTML with regexes.
不要捕捉整个事情。将非捕获组与
(?:...)
一起使用。这样你就可以进一步细分你匹配的部分和你捕获的部分。Don't capture the whole thing. Use non-capturing groups with
(?:...)
. This way you can further subdivide the part that you match and the part that you capture.这显然在正则表达式中不起作用,但您可以运行 split 对 $1 执行函数并获取结果数组的最后一个元素。
This obviously does not work inside the regex, but you could just run the split function on $1 and grab the last element of the resulting array.
怎么样:
What about: