Substring to Get Text after Second Period in Powershell

谁都会走 提交于 2020-04-11 04:38:11

问题


I am working with a Substring that will give me everything before the 2nd '.' I've worked with substrings before to get everything before the 1st '.'

$id = $file.Substring(0, $File.LastIndexof('.'))

and it works perfectly. For this second substring this is what I have thus far

$netVerShort = $netVer.Substring(0,  2nd ('.'))

but I'm certain this isn't right. The value I get from $netVer is 2.0.5727 and with this second substring I'm trying to get it to return 2.0


回答1:


Not necessarily the most efficient approach but you could use split for this and rejoin the elements you are looking for. Take the following string:

$stuff = "This.is the end. Bagels. are. awesome."

Now we split that on periods and then take the first two elements and join them back together.

PS C:\Users\mcameron> $stuff.Split(".",3)
This
is the end
 Bagels. are. awesome.

The 3 is to say that we only need to return 3 elements. Depending on how many periods you have this might be a good idea as to not waste time. Next we need to join the 2 elements you want.

PS C:\Users\mcameron> ($stuff.Split(".",3) | Select -Index 0,1) -join "."
This.is the end

To address your .SubString() approach you would need to check twice since you would have the first one then you would need to find the second.

PS C:\Users\mcameron> $stuff.Substring($stuff.IndexOf(".",$stuff.IndexOf(".") + 1))
. Bagels. are. awesome.

Get the first index with $stuff.IndexOf(".") then add 1 to the return. Using that value as the start index for the search of the next period. Then use that for the substring. Add 1 to the entire thing if you dont want the period to be part of the return.




回答2:


So, this is the first '.':

$x.IndexOf('.')

This is everything after the first '.':

$x.Substring($x.IndexOf('.') + 1)

This is the first '.' after the first '.' (so, the second period):

$x.Substring($x.IndexOf('.') + 1).IndexOf('.')

So, the length of string we want is:

$x.IndexOf('.') + 1 + $x.Substring($x.IndexOf('.') + 1).IndexOf('.')

So, we want the string, starting from the beginning with a length long enough to reach just before the second period:

$x.Substring(0, $x.IndexOf('.') + 1 + $x.Substring($x.IndexOf('.') + 1).IndexOf('.'))

So:

PS C:\> $x = '198.51.100.5';
PS C:\> $x.Substring(0, $x.IndexOf('.') + 1 + $x.Substring($x.IndexOf('.') + 1).IndexOf('.'))
198.51



回答3:


If you are working with version numbers, the System.Version class that can do the parsing for you:

$netVer = "2.0.5727"
$v = [version]$netVer
$netVerShort = "$($v.Major).$($v.Minor)"

will yield "2.0"




回答4:


Your question is unclear. Do you need to include the 2nd period? Do you want everything before the 2nd period or just what's between the 1st and 2nd period?

"th1 s.!s.a.test" -replace '^(.+?\..+?)\..*', '$1'
th1 s.!s

If you need to seperate them you could try this:

"th1 s.!s.a.test" -replace '^(.+?)\.(.+?)\..*', '$1'
th1 s

"th1 s.!s.a.test" -replace '^(.+?)\.(.+?)\..*', '$2'
!s

If you only need the major and minor version, try:

"2.0.5727" -replace '^(\d+\.\d+).*', '$1'
2.0

Personally, I would use the proper .Net solution that mjolinor suggested in your other question (which this is kinda a duplicate of).




回答5:


This is a variation of Matt's answer above, but using slightly different semantics. Again, we split and join the string:

("this.is the end.bagels.are good.oh yes" -split "\.")[0..1] -join "."

In this, we've replaced the select -index that Matt used with an array range [0..1] and use the native -split and -join operators. Is it any more efficient than Matt's? In terms of keystrokes, it is :-) But computationally probably not much (if any) difference.



来源:https://stackoverflow.com/questions/27513886/substring-to-get-text-after-second-period-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!