[英]Replace leading zeros with spaces
I have a text file of multiple records.我有一个包含多条记录的文本文件。 Each record has a field which has some number of leading zeros that I need to replace with that number of spaces.
每条记录都有一个字段,其中包含一些前导零,我需要用该数量的空格替换这些零。 A record will look like this:
记录将如下所示:
A206 000001204 X4609
I need the record to look like this:我需要记录看起来像这样:
A206 1204 X4609
I'm extremely unfamiliar with regex but the following regex seems to find the matches that I need:我对正则表达式非常陌生,但以下正则表达式似乎找到了我需要的匹配项:
\b0+
However, I have no idea how to do the replacement.但是,我不知道如何进行替换。 A ReplaceAll for Notepad++ would be awesome but I can also create a quick program in C#, Powershell, or Python if needed.
Notepad++ 的 ReplaceAll 会很棒,但如果需要,我也可以在 C#、Powershell 或 Python 中创建一个快速程序。 Can anyone give me some pointers on the regex for this?
任何人都可以给我一些关于正则表达式的指示吗?
Yes, \\b0+
would probably work.是的,
\\b0+
可能会起作用。
Here using the Regex.Replace()
method in C#
:这里使用
C#
的Regex.Replace()
方法:
using System.Text.RegularExpressions;
Regex.Replace(inputString, @"\b0+", m => "".PadLeft(m.Value.Length,' '));
The last argument to Replace()
is a simple lambda function that returns a string of the same length as the number of matched 0
s, but consisting only of spaces Replace()
的最后一个参数是一个简单的 lambda 函数,它返回一个长度与匹配的0
的数量相同的字符串,但只包含空格
You can do the same in PowerShell
, substituting a scriptblock
for the lambda function:您可以在
PowerShell
执行相同操作,用scriptblock
替换 lambda 函数:
PS C:\> $inputString = 'A206 000001204 X4609'
PS C:\> [regex]::Replace($inputString, '\b0+', {param($m) ' ' * $m.Value.Length})
A206 1204 X4609
Does this suffice?这足够了吗?
while (dataString.Contains(" 0")) // while data contains a zero after a space
dataString = dataString.Replace(" 0", " "); // Replace with two spaces
Though this doesn't use regex.虽然这不使用正则表达式。
I hope this helps.我希望这会有所帮助。
Using Npp:使用 NPP:
\\b0
\\b0
(a space) My Notepad++ regex suggestion is not as short as Toto's, but it is a little more strict.我的 Notepad++ regex 建议不像 Toto 那样简短,但更严格一些。
Find what: (?: \\K|\\G)0
找出什么:
(?: \\K|\\G)0
Replace with:替换为:
This will seek out a space, then forget it (compliments of \\K
-- the Keep metacharacter), then individually match each consecutive zero (compliments of \\G
-- the Continue metacharacter).这将寻找一个空格,然后忘记它(
\\K
赞美——Keep 元字符),然后单独匹配每个连续的零( \\G
赞美——Continue 元字符)。
Replace All will deliver the desired result.全部替换将提供所需的结果。
I am posting this to make this page a little more accommodating for future researchers who may have slightly different requirements.我发布此内容是为了使此页面更适合未来可能有略有不同要求的研究人员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.