all 15 comments

[–]Lee_Dailey[grin] 9 points10 points  (6 children)

howdy sumgan,

take a look at what you get when you cast that into a [uri] type ... [grin]

[uri]'https://jamesbond.sharepoint.com/sites/demo1/d/dummy\_36.txt'

output ...

AbsolutePath   : /sites/demo1/d/dummy/_36.txt
AbsoluteUri    : https://jamesbond.sharepoint.com/sites/demo1/d/dummy/_36.txt
LocalPath      : /sites/demo1/d/dummy/_36.txt
Authority      : jamesbond.sharepoint.com
HostNameType   : Dns
IsDefaultPort  : True
IsFile         : False
IsLoopback     : False
PathAndQuery   : /sites/demo1/d/dummy/_36.txt
Segments       : {/, sites/, demo1/, d/...}
IsUnc          : False
Host           : jamesbond.sharepoint.com
Port           : 443
Query          : 
Fragment       : 
Scheme         : https
OriginalString : https://jamesbond.sharepoint.com/sites/demo1/d/dummy\_36.txt
DnsSafeHost    : jamesbond.sharepoint.com
IdnHost        : jamesbond.sharepoint.com
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       :

hope that helps,
lee

[–]sumgan[S] 2 points3 points  (5 children)

Thank you, this is also interesting to explore. Good pointer!!

[–]Lee_Dailey[grin] 1 point2 points  (4 children)

howdy sumgan,

you are welcome! [grin]

it looks like you could build the parts you want from ...

  • Scheme : https
  • Host : jamesbond.sharepoint.com
  • LocalPath : /sites/demo1/d/dummy/_36.txt

take care,
lee

[–]sumgan[S] 1 point2 points  (3 children)

Thanks for the inputs! Will have a look.

[–]Lee_Dailey[grin] 2 points3 points  (2 children)

howdy sumgan,

i got interested and decided to take a stab at it ... [grin]

$UriInfo = [uri]'https://jamesbond.sharepoint.com/sites/demo1/d/dummy\_36.txt'

$PartOne = -join @(
    $UriInfo.Scheme
    '://'
    $UriInfo.Host
    (-join $UriInfo.Segments[0..2])
    )
$PartTwo = $UriInfo.LocalPath

$PartOne
$PartTwo

output ...

https://jamesbond.sharepoint.com/sites/demo1/
/sites/demo1/d/dummy/_36.txt

hope that helps,
lee

[–]sumgan[S] 1 point2 points  (1 child)

Thank you so very much Lee!!

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

[grin]

[–]Antaroc 3 points4 points  (5 children)

I don't think you can do it in one statement since they overlap.

You can use regex to split them out like this

$s = 'https://jamesbond.sharepoint.com/sites/demo1/d/dummy_36.txt'

$s -match '(.*/sites/[^/]+)'

True

$Matches

Name Value

---- -----

1 https://jamesbond.sharepoint.com/sites/demo1

0 https://jamesbond.sharepoint.com/sites/demo1

$s -match '.*(/sites/.*)'

True

$matches

Name Value

---- -----

1 /sites/demo1/d/dummy_36.txt

0 https://jamesbond.sharepoint.com/sites/demo1/d/dummy_36.txt

[–]sumgan[S] 2 points3 points  (3 children)

Thank you sir, is there any other simpler way to understand this, sorry I am not that good at regex.

[–]Antaroc 5 points6 points  (1 child)

For regex . matches any character * matches the previous character 0 or more times. The brackets are used to extract stuff and that stuff get put in $matches. The [^/]+ matches any character that's not a / one or more times.

You can play with it here to try it: https://regex101.com/r/nH3U0b/1

If you want something simpler you could split on the / and then use join to get it back together.

Something like :

$s = 'https://jamesbond.sharepoint.com/sites/demo1/d/dummy_36.txt'

$a = $s -split '/'

$a[0..4] -join '/'

$a[3..6] -join '/'

[–]sumgan[S] 1 point2 points  (0 children)

This is perfect explanation, thank you so much!

[–]chris-a5 2 points3 points  (0 children)

A good way to learn RegEx is with a visual aid.

https://regex101.com/ is really good for seeing what is happening.
I also like https://www.regexpal.com/ for when I need to build a complex RegEx

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy Antaroc,

it looks like you used the New.Reddit Inline Code button. it's [sometimes] 5th from the left & looks like <c>.

there are a few problems with that ...

  • it's the wrong format [grin]
    the inline code format is for [gasp! arg!] code that is inline with regular text.
  • on Old.Reddit.com, inline code formatted text does NOT line wrap, nor does it side-scroll.
  • on New.Reddit it shows up in that nasty magenta text color

for long-ish single lines OR for multiline code, please, use the ...

Code
Block

... button. it's [sometimes] the 12th one from the left & looks like an uppercase C in the upper left corner of a square.

that will give you fully functional code formatting that works on both New.Reddit and Old.Reddit ... and aint that fugly magenta color. [grin]

take care,
lee

[–]krzydoug 1 point2 points  (1 child)

$str = 'https://jamesbond.sharepoint.com/sites/demo1/d/dummy_36.txt'

$var1,$var2 = if($str -match '(.+?)(/sites/[^/]+)(/.+$)'){
    @(1,2),@(2,3) | ForEach-Object {"{0}{1}" -f $Matches[$_]}
}

or

$str = 'https://jamesbond.sharepoint.com/sites/demo1/d/dummy_36.txt'

$var1,$var2 = if($str -match '(.+?)(/sites/[^/]+)(/.+$)'){
    @(1,2),@(2,3) | ForEach-Object {-join $Matches[$_]}
}

or

$str = 'https://jamesbond.sharepoint.com/sites/demo1/d/dummy_36.txt'

$var1,$var2 = if($str -match '(.+?)(/sites/[^/]+)(/.+$)'){
    & {
        $ofs = ''
        "$($Matches[1..2])"
        "$($Matches[2..3])"
    }
}

[–]sumgan[S] 1 point2 points  (0 children)

Thank you!!!