powershell:如何删除每行文本中的第一个#?

时间:2016-06-21 01:52:57

标签: string powershell replace

E.g。我有一个这样的文本文件:

abc,#32432,he#llo
xyz,#989,wor#ld

我希望将其改为:

abc,32432,he#llo
xyz,989,wor#ld

从每行中删除第一个“#”。怎么做? 感谢。

2 个答案:

答案 0 :(得分:1)

要仅更改字符串中的第一个#,您可以使用像这样的正则表达式

"abc,#32432,he#llo" -replace '(.*?)#(.*$)', '${1}${2}'

Regexbuddy的解释

# (.*?)#(.*$)
# 
# Options: Case insensitive; Exact spacing; Dot doesn't match line breaks; ^$ match at line breaks; Parentheses capture
# 
# Match the regex below and capture its match into backreference number 1 «(.*?)»
#    Match any single character that is NOT a line break character (line feed) «.*?»
#       Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
# Match the character “#” literally «#»
# Match the regex below and capture its match into backreference number 2 «(.*$)»
#    Match any single character that is NOT a line break character (line feed) «.*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Assert position at the end of a line (at the end of the string or before a line break character) (line feed) «$»

答案 1 :(得分:1)

您可以使用简单的正则表达式替换非哈希值的哈希值,如下所示:

Get-Content input_file.txt | % {$_ -replace '(?<=^[^#]+)#'} | Set-Content output_file.txt