PowerShell Regex with "." PowerShell - regex

i want to replace a part of some Strings in a loop with PowerShell
example string
testvm029.vmxxx
I want to replace everything at .vmxxx. Every string has another length but all ends with .vm... So the result should be: testvm029
I tried the following Script:
Foreach($String in $Strings) {
$StringTest = $String -replace "(.vm)(.+)","$null"
}
Of course this kills my String on the first vm and not at .vm...
result:
test
how can i achieve my goal?
EDIT: .vm is not the last '.' of my String so something like that:
test123.vmabc.string
I want to cut it after .vm

You need to use
$String -replace '\.vm.*'
See the regex demo
The pattern will find the first .vm substring (with \.vm) and then match the rest of the string (with .*). The match will be replaced with an empty string (it is used by default, but you may write it explicitly, $String -replace '\.vm.*', '').

Related

regex to ignore all text after the fifth occurrence of a '\' backslash

I have been trying to get to write a regex that ignores all text post the fifth occurrence of backslash, e.g below text; language in question is powershell
\\Fileserver\Usershare$\Svr2008\Profiles\john.contoso.V2
I made multiple attempts but no avail, the best i could do was match everything post usershare$, using ([^\$]*$) which gives me \\Fileserver\Usershare$
I would like the output to be \\Fileserver\Usershare$\svr2008
Any help would be appreciated.
As suggested by Mik i tried ^\\\\([^\\]*\\){3} and used it as
$Parent= \\Fileserver\Usershare$\Svr2008\Profiles\john.contoso.V2
$test = $parent -replace '^\\\\([^\\]*\\){3}'
$test
I get the below
Svr2008\Profiles\john.contoso.V2
However i require the part that didn't match, am i doing something wrong?
Try this one (it assumes there are 5+ backslashes)
^\\\\([^\\]*\\){3}
It matches
\\Fileserver\Usershare$\Svr2008\ in \\Fileserver\Usershare$\Svr2008\Profiles\john.contoso.V2
After doing '\\Fileserver\Usershare$\Svr2008\Profiles\john.contoso.V2' -matches '^\\\\([^\\]*\\){3}', your desired string will be in $matches[0]
here's yet another way to get the job done. [grin]
what it does ...
puts the path in a $Var
splits on the \ chars
grabs the 1st five resulting items
joins them with \
the code ...
$Test = '\\Fileserver\Usershare$\Svr2008\Profiles\john.contoso.V2'
$Test.Split('\')[0..4] -join '\'
output = \\Fileserver\Usershare$\Svr2008

Replace Wildcard Value in String with PowerShell

I am trying to simply remove a known string and an unknown number in a string from a string using the Powershell replace command and can't quite figure the syntax for a wildcard out.
My input string looks like this:
MyCatalog_AB_24.xml
However, the number is dynamic and won't always be 24.
And I need to wind up with:
MyCatalog.xml
So, I need to remove anything between MyCatalog and .xml (essentially the _AB_## part).
Here's the commands I've tried:
$_ -replace 'MyCatalog_AB_*.xml', 'MyCatalog.xml'
$_ -replace 'MyCatalog*.xml', 'MyCatalog.xml'
set num='\d'
$_ -replace 'MyCatalog_AB_%num%.xml', 'MyCatalog.xml'
I know I should be using some sort of regular expression, but I have some working code that someone else wrote that does something similar by just inserting an * where the wildcard data is.
Any help would be appreciated.
You may use
$_ -replace 'MyCatalog_AB_\d+\.xml', 'MyCatalog.xml'.
\d+ matches one or more digits, and \. matches a literal dot.

How to filter unwanted parts of a PowerShell string with Regex and replace?

I am confused about the workings of PowerShell's -replace operator in regards to its use with regex. I've looked for documentation online but can't find any that goes into more detail than basic use: it looks for a string, and replaces that string with either another string (if defined) or nothing. Great.
I want to do the same thing as the person in this question where the user wants to extract a simple program name from a complex string. Here is the code that I am trying to replicate:
$string = '% O0033(SUB RAD MSD 50R III) G91G1X-6.4Z-2.F500 G3I6.4Z-8.G3I6.4 G3R3.2X6.4F500 G91G0Z5. G91G1X-10.4 G3I10.4 G3R5.2X10.4 G90G0Z2. M99 %'
$program = $string -replace '^%\sO\d{4}\((.+?)\).+$','$1'
$program
SUB RAD MSD 50R III
As you can see the output string is the string that the user wants, and everything else is filtered out. The only difference for me is that I want a string that is composed of six digits and nothing else. However when I attempt to do it on a string with my regex, I get this:
$string2 = '1_123456_1'
$program2 = $string -replace '(\d{6})','$1'
$program2
1_123456_1
There is no change. Why is this happening? What should my code be instead? Furthermore, what is the $1 used for in the code?
The -replace operator only replaces the part of the string that matches. A capture group matches some subset of the match (or all of it), and the capture group can be referenced in the replace string as you've seen.
Your second example only ever matches that part you want to extract. So you need to ensure that you match the whole string but only capture the part you want to keep, then make the replacement string match your capture:
$string2 = '1_123456_1'
$program2 = $string -replace '\d_(\d{6})_\d','$1'
$program2
How you match "the rest of the string" is up to you; it depends on what could be contained in it. So what I did above is just one possible way. Other possible patterns:
1_(\d{6})_1
[^_]*_(\d{6})_[^_]*
^.*?(\d{6}).*?$
Capturing groups (pairs of unescaped parentheses) in the pattern are used to allow easy access to parts of a match. When you use -replace on a string, all non-overlapping substrings are matched, and these substrings are replaced/removed.
In your case, -replace '(\d{6})', '$1' means you replace the whole match (that is equal to the first capture, since you enclosed the whole pattern with a capturing group) with itself.
Use -match in cases like yours when you want to get a part of the string:
PS> $string2 = '1_123456_1'
PS> $string2 -match '[0-9]{6}'
PS> $Matches[0]
123456
The -match will get you the first match, just what you want.
Use -replace when you need to get a modified string back (reformatting a string, inserting/removing chars and suchlike).

Regular Expression - Perl

I am trying to get the a sub string from a string using regular expression but it getting error as my regular expression is not working. Can any one help me out in writing correct one :
Here is the Pattern on which i am trying to write the regular expression :
MSM8_BD_V4.3_1-1_idle-Kr_Run3.xlsx
MSM8_BD_V4.3_2-6_mp3-Kr_Run2.xlsx
MSM8_BD_V4.3_Camera_snap-7.xlsx
MSM8_BD_V4.3_Camera_snap-8.xlsx
MSM8_BD_V4.3_Radio_202.16-0.xlsx
I am trying to get the bold part of the substring .
below is the Regular expression i tried:
my $line = "MSM8939_BD_V4.3_1-1_idle-Kratos_Run3.xlsx";
my ($captured) = $line =~ /MSM8939_BD_V4\.\3\_[d]*(.+?)\w/gx;
print "$captured\n";
[d] matches nothing but the literal letter d. You want \d, without the brackets, to match a digit. However, it looks like you also want to include underscores. That would be [\d_].
Try this:
/^MSM8_BD_V4\.3_[\d_]*-?([^-]+)/
If I run this on your input (with e.g. perl -nE 'say $1 if /^MSM8_BD_V4\.3_[\d_]*-?([^-]+)/'), I get this output:
1_idle
6_mp3
Camera_snap
Camera_snap
Radio_202.16
my $line = "MSM8939_BD_V4.3_1-1_idle-Kratos_Run3.xlsx";
for (qw(
MSM8939_BD_V4.3_1-1_idle-Kratos_Run3.xlsx
MSM8939_BD_V4.3_2-6_mp3-Kratos_Run2.xlsx
MSM8939_BD_V4.3_Camera_snap-7.xlsx
MSM8939_BD_V4.3_Camera_snap-8.xlsx
MSM8939_BD_V4.3_Radio_202.16-0.xlsx
)) {
my ($captured) = ($_ =~ /.*[-_]([^\W_]+_[\w.]+)-/gx);
print "$captured\n";
}
Use a greedy pattern to go as far as possible, then grab the last two strings that look like what you want which are still followed by a hyphen.
As does the other answer which was just edited while I was typing, this produces:
1_idle
6_mp3
Camera_snap
Camera_snap
Radio_202.16
This one may be more general in that the beginning of the substring is not hard-coded, i.e., you could use it in other cases which did not necessarily start with MSM8_BD_V4.3.

Need Regex to parse String

Using a regular expression, I want to parse a String like "DOCID = 1234567 THIS IS TEST" and remove the remaining String after the numbers.
How can I do this using the VIM editor?
In Perl:
$str =~ s/(= \d+).*$/$1/;
In php:
$str = preg_replace('/(= \d+).*$/', "$1", $str);
That will do the job:
:%s/\d\+\zs.*
Explanation:
% use the whole buffer, you can omit this if you want to change current line only
s the substitute command
\d\+ match as many numbers
\zs set the start of match here
.* everything else
you can omit the replacement string because you want to delete the match
In VIM, in command mode (press ESC), write :
:s/\([^0-9]\+[0-9]\+\).*/\1/
This will do the job.
If you want to do all replacement possible, then :
:s/\([^0-9]\+[0-9]\+\).*/\1/g
in java string.replaceFirst("(= \\d+).*$","\\1");