Reputation: 246
I want to find out regex for \\mudit.bah.*[ps1|bat]
that means path can be like \\mudit.bah.xyz123.ps1
or \\mudit.bah.xyz123.bat
xyz123
can be anything.
I am using https://rextester.com/tester and trying it however I am able to do till \\{2}mudit.bah.
. I am not sure how what to use for *
which can be multiple character with .(dots)
or without .(dots)
.
Any help on this greatly appreciated.
Upvotes: 1
Views: 6991
Reputation: 486
for a regex match like you requested use:
^mudit.bah.*.(ps1|bat)
get-childitem | Where-Object {$_.Name -match "^mudit\.bah.*.(ps1|bat)"}
This will get the files that start with mudit.bah. and end in ps1 or bat. The .* after 'bah' will match any character, including numbers and symbols.
To do this without regex:
Get-ChildItem | Where-Object {($_.Name -like ("mudit.bah." + "*" + ".ps1")) -or ($_.Name -like ("mudit.bah." + "*" + ".bat"))}
Upvotes: 1
Reputation: 627022
First of all, [ps1|bat]
matches a single char, p
, s
, 1
, b
, a
or t
. To make a group that matches either ps1
or bat
, you need a grouping construct, (ps1|bat)
or (?:ps1|bat)
(non-capturing group).
To match any char as many times as possible other than newline, you may use .*
. To restrict it to any char but a dot, use a negated character class, [^.]
. Notet that [^.]*
will match 0 or more chars other than a dot while [^.]+
will match 1 or more.
Hence, you may use this regex to allow any chars between \\mudit.bah.
and ps1
or bat
:
^\\{2}mudit\.bah\..*\.(?:ps1|bat)$
Or, with the restriction to only one no-dot part in between them:
^\\{2}mudit\.bah\.[^.]*\.(?:ps1|bat)$
See the regex demo.
Details
^
- start of string\\{2}
- two backslashesmudit\.bah\.
- a mudit.bah.
substring[^.]+\.
- any 1+ chars other than .
and then a dot(?:ps1|bat)
- either ps1
or bat
$
- end of string.Upvotes: 1