简体   繁体   English

在Powershell中使用regex替换字符串的第一个匹配项

[英]Replace the first occurrence of the string using regex in powershell

I was working some replace string using regex in powershell. 我在Powershell中使用正则表达式工作一些替换字符串。 But When I try to implement the first occurrence in the stringto replace a word. 但是,当我尝试在字符串中实现第一次出现以替换单词时。 It does not work. 这是行不通的。

(((Grant Execute.*)\\n+go)(\\n)?) This is my working regex if I want to replace all the matches. (((Grant Execute.*)\\n+go)(\\n)?)如果我想替换所有匹配项,这是我的正则表达式。

^(((Grant Execute.*)\\n+go)(\\n)?) I used this when I want to replace the first occurrence of the string... ^(((Grant Execute.*)\\n+go)(\\n)?)我想替换字符串的第一个匹配项时使用了这个...

Below is the string that I want to replace. 下面是我要替换的字符串。 This data is came from a batch file. 此数据来自批处理文件。 Grant Execute on dbo.Table to server1 go Grant Execute on dbo.Table to server2 go Grant Execute on dbo.Table to server2

试试这个模式:

(?s)(?<!go\s*)Grant Execute.*?go(?!\sGrant)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM