1

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

asked Oct 9, 2019 at 16:52

4 Answers 4

2

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]
 # ... 
}
answered Oct 9, 2019 at 17:14
Sign up to request clarification or add additional context in comments.

Comments

1

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:

enter image description here

Details

  • ^ - start of string
  • [^.]+ - 1+ chars other than .
  • \. - a dot
  • | - or
  • \. - a dot
  • [^.]+ - 1+ chars other than .
  • $ - end of string.
answered Oct 9, 2019 at 16:54

Comments

1

You can use -match to capture your desired output using regex

$string ="SIT.com.local.test.stack.properties"
$string -match "^.*?\.(.+)\.[^.]+$"
$Matches.1
answered Oct 9, 2019 at 17:01

1 Comment

Thank you @r007ed and mtnielsen, both answers worked
1

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).

answered Oct 9, 2019 at 17:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.