What is the equivalent to \K for regex in Powershell?

久未见 提交于 2019-12-11 05:56:59

问题


I have created a regular expression on regex101 that works as expected, however the problem is that one part is apparently not valid in Powershell; the \K.

Essentially I am looking for instances of a string, and returning the entire word after the instance of the string. So here is an example:

\btest\s+\K\S+

This finds every example of the word test and returns the word after it. I did try experimenting with Lookaheads and Lookbehinds, while some of those do work, they are still either returning test or additional unnecessary characters.

Is there a way to replicate the \K in powershell? Or even better, any add-ons that will allow PowerShell to use the \K?


回答1:


What about something like this: (?<=\btest\b)\s+(\w+)\S+

A positive look-behind for test followed by the rest of the stuff you were looking for, and a capture group for the following word.

PowerShell uses the .Net RegEx engine, and it doesn't support \K as you know it (it's used for referencing named capture groups in .Net). You can't change that.

You could possibly use some third party implementation of regex that's compatible with .Net, but it seems unlikely that there is such a thing since in general .Net has a pretty good and full-featured engine.



来源:https://stackoverflow.com/questions/46611602/what-is-the-equivalent-to-k-for-regex-in-powershell

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