Replace Wildcard Value in String with PowerShell - regex

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.

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

Matching strings with and without escape characters with RegEx

I have different distinguished names from Active Directory objects and need to filter out escape characters when splitting those dn´s into simple names.
I already have a string -split of PowerShell in place, but this does not filter out escape characters. I´ve tried regex with a positive lookbehind but i do need in this case something like a optional positive lookbehind? Maybe I'm just thinking too complicated.
String examples:
OU=External,OU=T1,OU=\+TE,DC=test,DC=dir
OU=\#External,OU=T1,OU=\+TE,DC=test,DC=dir
OU=\+External,OU=T1,OU=\+TE,DC=test,DC=dir
Because + and # are escaped but are the actual name of those objects, I need to remove the escape characters
With following PowerShell it is possible to get the name of the object
($variable -split ',*..=')[1]
Actual Result:
External
\#External
\+External
Expected Result:
External
#External
+External
It is possible to use regex with $variable -creplace "REGEX" but I cant find a regex which fits all those cases.
My try was: (?<=OU=\\).+?(?=,OU=) but just matches if the \ is there
I need this name for the object creation inside Active Directory.
With minimal change you could just add the slash as optional in your current regex. You already do something similar with the leading comma
"OU=\#External,OU=T1,OU=\+TE,DC=test,DC=dir" -split ',?..=\\?'
You could take that farther if you were just going for the first section but that answers your basic question. There is likely other efficiencies to be made but probably not worth it.
For extracting the first OU name from a DN while removing an optional leading backslash at the same time you can use a regular expression like this:
OU=\\?(.*?), *..=.*$
Demonstration:
$dn1 = 'OU=External,OU=T1,OU=\+TE,DC=test,DC=dir'
$dn2 = 'OU=\#External,OU=T1,OU=\+TE,DC=test,DC=dir'
$dn3 = 'OU=\+External,OU=T1,OU=\+TE,DC=test,DC=dir'
$dn1 -replace 'OU=\\?(.*?), *..=.*$', '$1' # output: External
$dn2 -replace 'OU=\\?(.*?), *..=.*$', '$1' # output: #External
$dn3 -replace 'OU=\\?(.*?), *..=.*$', '$1' # output: +External

PowerShell Regex with "." PowerShell

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.*', '').

Regular expression replace all occurrences of any word beginning with a certain substring

Working with Windows PowerShell, I am trying to replace all occurrences of a certain word pattern in a big string, by appending a suffix.
$string = "The string that contains several occurrences of ab_abcde, ab_abcde_FOO, ab_vwxyz, ab_vwxyz_BAR, etc"
I want to append the suffix "_XXX" to each one of these matched words in $string
Since I expect all my matches to start with either ab_abcde or ab_vwxyz, I have tried:
$string -replace "(ab_abcde|ab_vwxyz)(.?)\s+", '$1$2_XXX'
and a million other variations, to no avail.
I have spent all day hunting at SO, but haven't found a solution that works well. I would appreciate a little assistance.
PS: Since my $string is actually the output of mysqldump, some of my search words will be wrapped in between grave-accent(`) like so: `ab_abcde`, and I would like to match them. I am not sure how much complexity this might bring, considering that this is also the PowerShell escape character.
Thank you.
Something like
$string -replace "((?:ab_abcde|ab_vwxyz)\w*)", '$1_XXX'
Regex Demo
Like this?
$string = "The string that contains several occurrences of ab_abcde, ab_abcde_FOO, ab_vwxyz, ab_vwxyz_BAR, etc"
$string -replace '(ab_abcde.*?\b|ab_vwxyz.*?\b)', '$1_XXX'
The string that contains several occurrences of ab_abcde_XXX, ab_abcde_FOO_XXX, ab_vwxyz_XXX, ab_vwxyz_BAR_XXX, etc
Edit: updated after additional comments posted.

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.