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

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

问题描述

使用正则表达式(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

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天全站免登陆