regular expression to match everything until the last occurrence of /

前端 未结 3 612
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 09:08

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?

3条回答
  •  春和景丽
    2020-11-30 09:35

    What you want to do is match greedily, the longest possible match of the pattern, it is default usually, but match till the last instance of '/'.

    That would be something like this:

     .*\/
    

    Explanation:

    . any character
    * any and all characters after that (greedy)
    \/ the slash escaped, this will stop at the **last** instance of '/'
    

    You can see it in action here: http://regex101.com/r/pI4lR5

提交回复
热议问题