在Powershell中,正则表达式的\ K等于什么? [英] What is the equivalent to \K for regex in Powershell?

查看:268
本文介绍了在Powershell中,正则表达式的\ K等于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在regex101上创建了一个正则表达式,该正则表达式可以按预期工作,但是问题是,其中一部分显然在Powershell中无效; \K.

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+

这会找到单词test的每个示例,并在其后返回单词.我确实尝试过使用Lookaheads和Lookbehinds进行实验,尽管其中一些确实起作用,但它们仍然返回test或其他不必要的字符.

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.

有没有办法在Powershell中复制\K?甚至更好的是,任何允许PowerShell使用\K的加载项?

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

推荐答案

test的正向查找,然后是您要查找的其余内容,以及随后的单词的捕获组.

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使用.Net RegEx引擎,并且它不支持您所知道的\K (用于引用.Net中的命名捕获组).您无法更改.

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.

您可能会使用一些与.Net兼容的正则表达式的第三方实现,但似乎不太可能出现这种情况,因为.Net通常具有相当不错的功能齐全的引擎.

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.

这篇关于在Powershell中,正则表达式的\ K等于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆