Home > Software design >  Regex - How to split this string in two parts
Regex - How to split this string in two parts

Time:12-11

I would like to split this string in two parts :

C:\Program Files\Autodesk\DWG TrueView 2021 - English\Setup\en-us\Setup\Setup.exe /P {28B89EEF-4356-0409-0100-CF3F3A09B77D} /M AOEM /language en-US

I would like that it splits always after Setup.exe, so it should be :

$part1 = "C:\Program Files\Autodesk\DWG TrueView 2021 - English\Setup\en-us\Setup\Setup.exe"
$part2 = "/P {28B89EEF-4356-0409-0100-CF3F3A09B77D} /M AOEM /language en-US"

What should be the function to do that ?

Thank you

CodePudding user response:

$string = "C:\Program Files\Autodesk\DWG TrueView 2021 - English\Setup\en-us\Setup\Setup.exe /P {28B89EEF-4356-0409-0100-CF3F3A09B77D} /M AOEM /language en-US" 
$array = $string -Split "(Setup.exe)"
$array[0] $array[1]
$array[2]

There can be other simple ways. This is One way of doing it.

CodePudding user response:

Here's a few alternatives for you:

$string = 'C:\Program Files\Autodesk\DWG TrueView 2021 - English\Setup\en-us\Setup\Setup.exe /P {28B89EEF-4356-0409-0100-CF3F3A09B77D} /M AOEM /language en-US'

Method 1: use -split

$part1, $part2 = $string -split '^(. \.exe)\s(. )$'
$part1   # --> C:\Program Files\Autodesk\DWG TrueView 2021 - English\Setup\en-us\Setup\Setup.exe
$part2   # --> /P {28B89EEF-4356-0409-0100-CF3F3A09B77D} /M AOEM /language en-US

Method 2: use [regex].Match()

$part1, $part2 = ([regex]'(?i)^(. \.exe)\s(. )$').Match($string).Groups[1..2].Value
$part1   # --> C:\Program Files\Autodesk\DWG TrueView 2021 - English\Setup\en-us\Setup\Setup.exe
$part2   # --> /P {28B89EEF-4356-0409-0100-CF3F3A09B77D} /M AOEM /language en-US

Method 3: use -replace combined with method .Split()

$part1, $part2 = ($string -replace '^(. \.exe)\s(. )$', '$1|$2').Split('|')
$part1   # --> C:\Program Files\Autodesk\DWG TrueView 2021 - English\Setup\en-us\Setup\Setup.exe
$part2   # --> /P {28B89EEF-4356-0409-0100-CF3F3A09B77D} /M AOEM /language en-US

Method 4: use string methods .IndexOf() and .Substring()

$split = $string.IndexOf('.exe')
if ($split -ge 0) {
    $part1 = $string.Substring(0, $split   4)
    $part2 = $string.Substring($split   5)
}

$part1   # --> C:\Program Files\Autodesk\DWG TrueView 2021 - English\Setup\en-us\Setup\Setup.exe
$part2   # --> /P {28B89EEF-4356-0409-0100-CF3F3A09B77D} /M AOEM /language en-US

CodePudding user response:

Just splitting on whitespace and putting them back together. You might not even have to join together the $part2 array depending on your use as an -argumentlist or with the call operator.

$string = 'C:\Program Files\Autodesk\DWG TrueView 2021 - English\Setup\en-us\Setup\Setup.exe /P {28B89EEF-4356-0409-0100-CF3F3A09B77D} /M AOEM /language en-US'
$split = -split $string

$split
C:\Program
Files\Autodesk\DWG
TrueView
2021
-
English\Setup\en-us\Setup\Setup.exe
/P
{28B89EEF-4356-0409-0100-CF3F3A09B77D}
/M
AOEM
/language
en-US

$part1 = $split[0..5] -join ' '
$part2 = $split[6..$split.length] -join ' '
# $part2 = $split[6..$split.length]
  • Related