Regex Last occurrence?

妖精的绣舞 提交于 2019-12-17 04:53:38

问题


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]


回答1:


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

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

See it here on Regexr




回答2:


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



回答3:


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.




回答4:


You can try anchoring it to the end of the string, something like \\[^\\]*$. Though I'm not sure if one absolutely has to use regexp for the task.




回答5:


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




回答6:


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




回答7:


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

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

[Regex Demo]



来源:https://stackoverflow.com/questions/8374742/regex-last-occurrence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!