Discussion:
Replace string
(too old to reply)
Armando Torres
2014-12-23 18:32:55 UTC
Permalink
I would like to replace all the number on the left side of the underscore with blank space:
12_
123_
1_
I am using the following and it does not work; any help is appreciated.
(get-content D:\Temp\Test1.txt) | foreach-object {$_ -replace '*_', ' '} | set-content D:\Temp\Test1.txt
Luuk
2014-12-24 11:41:39 UTC
Permalink
Post by Armando Torres
12_
123_
1_
I am using the following and it does not work; any help is appreciated.
(get-content D:\Temp\Test1.txt) | foreach-object {$_ -replace '*_', ' '} | set-content D:\Temp\Test1.txt
foreach-object {$_ -replace '[0-9]',' ' }
Jürgen Exner
2015-01-04 18:12:05 UTC
Permalink
Post by Luuk
Post by Armando Torres
12_
123_
1_
I am using the following and it does not work; any help is appreciated.
(get-content D:\Temp\Test1.txt) | foreach-object {$_ -replace '*_', ' '} | set-content D:\Temp\Test1.txt
foreach-object {$_ -replace '[0-9]',' ' }
Which will remove any digit anywhwere in the string, not just to the
left of the underscore as the OP asked.

jue
Jürgen Exner
2015-01-04 18:12:41 UTC
Permalink
On Tue, 23 Dec 2014 10:32:55 -0800 (PST), Armando Torres
Post by Armando Torres
12_
123_
1_
I am using the following and it does not work; any help is appreciated.
(get-content D:\Temp\Test1.txt) | foreach-object {$_ -replace '*_', ' '} | set-content D:\Temp\Test1.txt
Your RE '*_' is wrong. The star '*' is a multiplier, so you are trying
to match zero or more instances of nothing which are then followed by
the underscore character.
You need to specify the basic item of which you would like to match zero
or more instances, e.g. any character ('.*_') or digits ('\d*_') or
whatever it is you want matched.

jue
Luuk
2015-01-10 12:47:10 UTC
Permalink
Post by Jürgen Exner
On Tue, 23 Dec 2014 10:32:55 -0800 (PST), Armando Torres
Post by Armando Torres
12_
123_
1_
I am using the following and it does not work; any help is appreciated.
(get-content D:\Temp\Test1.txt) | foreach-object {$_ -replace '*_', ' '} | set-content D:\Temp\Test1.txt
Your RE '*_' is wrong. The star '*' is a multiplier, so you are trying
to match zero or more instances of nothing which are then followed by
the underscore character.
You need to specify the basic item of which you would like to match zero
or more instances, e.g. any character ('.*_') or digits ('\d*_') or
whatever it is you want matched.
jue
In below examples i did not replace with a space ' ', but with a '.'
(this makes it hopefully more readable)

PS C:\temp> (get-content C:\Temp\Test1.txt) | foreach-object {$_
-replace '\d*_', '.'}
.
.
.
PS C:\temp> (get-content C:\Temp\Test1.txt) | foreach-object {$_
-replace '\d*', '.'}
.._.
.._. .
.._.
PS C:\temp> (get-content C:\Temp\Test1.txt) | foreach-object {$_
-replace '\d[^_]*', '.'}
._
._
._


The '\d*_' variant also replaces the '_'.


I do not understand the output of the second variant '\d*'

My input file is form the original post like this:
PS C:\temp> type test1.txt
12_
123_
1_
PS C:\temp>
issdr
2015-02-15 12:06:15 UTC
Permalink
[...]
Post by Luuk
Post by Jürgen Exner
You need to specify the basic item of which you would like to match zero
or more instances, e.g. any character ('.*_') or digits ('\d*_') or
whatever it is you want matched.
[...]
Post by Luuk
In below examples i did not replace with a space ' ', but with a '.'
(this makes it hopefully more readable)
PS C:\temp> (get-content C:\Temp\Test1.txt) | foreach-object {$_
-replace '\d*_', '.'}
2nd argument for -replace is not a regex, thus you need to specify a
literal replacement (actually you may use variables to, captured text for
example, but i'm keeping it simple)

if i understand the op's intention,

-replace '^\d+_', ' '

should do

hth
--
np: no song
Loading...