简体   繁体   English

如何替换 powershell 中第二次出现的字符?

[英]How to replace the second occurrence of a character in powershell?

I'm really struggling with a task that is probably easier than I image it.我真的在努力完成一项可能比我想象的更容易的任务。

I have a file with a lot of lines:我有一个包含很多行的文件:

eg例如

"numbers TG alongstring" 
"numbers 100 alongstring" 
"numbers 120 alongstring" 
"numbers AF alongstring" 
"numbers 123 alongstring" 

I want to replace the second occurrence of我想替换第二次出现 (space) with (空格)与 (two spaces) so that the longstrings line up with each other. (两个空格)使长字符串彼此对齐。 The strings that are 2 characters long are always numbers. 2 个字符长的字符串始终是数字。

Something like this:像这样的东西:

"numbers TG  alongstring" 
"numbers 100 alongstring" 
"numbers 120 alongstring" 
"numbers AF  alongstring" 
"numbers 123 alongstring" 

A thing to note is that the numbers at the beginning are unique along 4000 lines and the longstrings are also not the same length.需要注意的是,开头的数字在 4000 行中是唯一的,并且长字符串的长度也不相同。

What I have so far:到目前为止我所拥有的:

foreach ($Line in $file) {
if ($Line[10] -eq " ") {
    $Line.Replace(" ", "  ")
    
    }

}

[10] is the index of the space I want to replace. [10] 是我要替换的空间的索引。

This replaces all the single spaces with double spaces I tried a lot of things but haven't had any luck so far.这用双空格替换了所有单空格我尝试了很多东西,但到目前为止还没有任何运气。

Any help is greatly appreciated.任何帮助是极大的赞赏。

Replace (?m)(^[^\n\r ]* [^\n\r ]{2}) (it contains a trailing space) with $1 (it contains a double trailing space).(?m)(^[^\n\r ]* [^\n\r ]{2}) (它包含一个尾随空格)替换$1 (它包含一个双尾随空格)。

This regex replaces the second single-space occurrence with a double space only if it is preceded by two non-space characters.仅当前面有两个非空格字符时,此正则表达式才会用双空格替换第二个单空格出现。

  • (?m) is the multiline flag (?m)是多行标志
  • ^ matches the beginning of the line (because of (?m) , otherwise it would metche the very beginning of the string) ^匹配行的开头(因为(?m) ,否则它会匹配字符串的开头)
  • [^\n\r ]* matches zero or more characters that are neither a space nor a new line [^\n\r ]*匹配零个或多个既不是空格也不是换行符的字符
  • [^\n\r ]{2} matches exactly two characters that are neither a space nor a new line [^\n\r ]{2}恰好匹配两个既不是空格也不是换行符的字符
  • (...) is a capturing group, and you refer to it with $1 (...)是一个捕获组,你用$1引用它

See a demo here . 在此处查看演示。

Among other solutions you could use在您可以使用的其他解决方案中

^((?:[^\n ]* (?! )){2})

And replace this with $1 .并将其替换为$1
See a demo on regex101.com .请参阅regex101.com 上的演示

Try testing the .LastIndexOf( ' ' ) return.尝试测试.LastIndexOf( ' ' )返回。 if the last index of space is 10 you know you are missing a space.如果空间的最后一个索引是 10,你就知道你缺少一个空间。 Then you can use the .Insert() method to add it in the same position.然后可以使用.Insert()方法将其添加到同一个 position 中。

Maybe something like:也许是这样的:

foreach ($Line in $file) {
    if ($Line.LastindexOf( ' ' ) -eq 10 ) {
        $Line.Insert( 10, ' '  )
        }
    }

Something like this could work and you should be able to come up with a generalized pattern for a match criteria.像这样的东西可以工作,你应该能够为匹配标准提出一个通用模式。

Get-Content $file | Foreach-Object {
    # matches <nonspaces><space><nonspaces><spaces><rest of string>
    if ($_ -match '^([^ ]+ \S+ +)(.*)$') {
        # $matches.1 is everything matching in the first ()
        # $matches.2 is everything matching in the second ()
        # Assumes alongstring desirably lines up at position 13 starting from 0. Change 13 to the appropriate number if necessary
        ($matches.1).PadRight(13,' ') + $matches.2
    } else {
        $_ 
    }
}

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

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