Can you please help me to get the desired output, where SIT is the environment and type of file is properties, i need to remove the environment and the extension of the string.
#$string="<ENV>.<can have multiple period>.properties
*$string ="SIT.com.local.test.stack.properties"
$b=$string.split('.')
$b[0].Substring(1)*
Required output : com.local.test.stack //can have multiple period
4 Answers 4
This should do.
$string = "SIT.com.local.test.stack.properties"
# capture anything up to the first period, and in between first and last period
if($string -match '^(.+?)\.(.+)\.properties$') {
$environment = $Matches[1]
$properties = $Matches[2]
# ...
}
Comments
You may use
$string -replace '^[^.]+\.|\.[^.]+$'
This will remove the first 1+ chars other than a dot and then a dot, and the last dot followed with any 1+ non-dot chars.
See the regex demo and the regex graph:
Details
^
- start of string[^.]+
- 1+ chars other than.
\.
- a dot|
- or\.
- a dot[^.]+
- 1+ chars other than.
$
- end of string.
Comments
You can use -match to capture your desired output using regex
$string ="SIT.com.local.test.stack.properties"
$string -match "^.*?\.(.+)\.[^.]+$"
$Matches.1
1 Comment
You can do this with the Split operator also.
($string -split "\.",2)[1]
Explanation:
You split on the literal .
character with regex \.
. The ,2
syntax tells PowerShell to return 2 substrings after the split. The [1]
index selects the second element of the returned array. [0]
is the first substring (SIT
in this case).