正则表达式匹配所有内容,直到最后一次出现/ [英] regular expression to match everything until the last occurrence of /

查看:145
本文介绍了正则表达式匹配所有内容,直到最后一次出现/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用正则表达式(Ant中的replaceregexp)匹配(然后替换)从行首到最后一个斜杠(包括最后一个斜杠)的所有内容?

Using a regular expression (replaceregexp in Ant) how can I match (and then replace) everything from the start of a line, up to and including the last occurrence of a slash?

我需要从以下任何一个开始:

What I need is to start with any of these:

../../replace_this/keep_this

../../replace_this/keep_this

../replace_this/replace_this/Keep_this

../replace_this/replace_this/Keep_this

/../../replace_this/replace_this/Keep_this

/../../replace_this/replace_this/Keep_this

并将它们变成这个:

什么_我_添加了_保持

what_I_addedKeep_this

似乎应该很简单,但我不明白.我已经制作了正则表达式来标识最后一个斜杠并从那里匹配到行尾,但是我需要的是一个可以匹配从行首到最后一个斜杠的所有东西的正则表达式,所以我可以全部替换掉

It seems like it should be simple but I'm not getting it. I've made regular expressions that will identify the last slash and match from there to the end of the line, but what I need is one that will match everything from the start of a line until the last slash, so I can replace it all.

这是用于Ant构建文件的文件,该文件正在读取一堆.txt文件并转换其在其中找到的任何链接.我只想使用replaceregexp,而不是变量或属性.如果可能的话.

This is for an Ant build file that's reading a bunch of .txt files and transforming any links it finds in them. I just want to use replaceregexp, not variables or properties. If possible.

推荐答案

您可以匹配以下内容:

.*\/

并替换为您的文本.

演示

这篇关于正则表达式匹配所有内容,直到最后一次出现/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆