Regex Last occurrence?

2020-01-24 07:25发布

I'm trying to catch the last part after the last backslash
I need the \Web_ERP_Assistant (with the \)

My idea was :

C:\Projects\Ensure_Solution\Assistance\App_WebReferences\Web_ERP_WebService\Web_ERP_Assistant


\\.+?(?!\\)      //  I know there is something with negative look -ahead `(?!\\)`

But I can't find it.

[Regexer Demo]

标签: regex path
7条回答
来,给爷笑一个
2楼-- · 2020-01-24 07:31

What about this regex: \\[^\\]+$

查看更多
别忘想泡老子
3楼-- · 2020-01-24 07:35

One that worked for me was:

.+(\\.+)$

Try it online!

Explanation:

.+     - any character except newline
(      - create a group
 \\.+   - match a backslash, and any characters after it
)      - end group
$      - this all has to happen at the end of the string
查看更多
做个烂人
4楼-- · 2020-01-24 07:38

A negative look ahead is a correct answer, but it can be written more cleanly like:

(\\)(?!.*\\)

This looks for an occurrence of \ and then in a check that does not get matched, it looks for any number of characters followed by the character you don't want to see after it. Because it's negative, it only matches if it does not find a match.

查看更多
Viruses.
5楼-- · 2020-01-24 07:42

I used below regex to get that result also when its finished by a \

(\\[^\\]+)\\?$

[Regex Demo]

查看更多
冷血范
6楼-- · 2020-01-24 07:43

If you don't want to include the backslash, but only the text after it, try this: ([^\\]+)$ or for unix: ([^\/]+)$

查看更多
倾城 Initia
7楼-- · 2020-01-24 07:47

Your negative lookahead solution would e.g. be this:

\\(?:.(?!\\))+$

See it here on Regexr

查看更多
登录 后发表回答