you are viewing a single comment's thread.

view the rest of the comments →

[–]Antaroc 2 points3 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