A Regex pattern to split (possibly negative) integers separated with a hyphen - powershell

I need to split (possibly negative) integers separated with a hyphen and then turn those numbers into a range. I can do the range part, the "split" boggles me.
"8-12" # output = 8, 12
"-4--2" # output = -4, -2

Depending on your inputs, following could suffice
"8-12" -replace '(\d)-', '$1, '
"-4--2" -replace '(\d)-', '$1, '
The gist of this is to search for a decimal, capturing it in a group, followed by a hyphen. Replace each match with the captured group (the decimal), a comma and a space.

Use a lookbehind assertion to only split on - when preceded by a digit:
PS C:\> '-4--12' -split '(?<=\d)-'
-4
-12

Related

Renaming the 4th character through the 12th

Here is what I have.
get-childitem "\\myfileserver\out\*" | foreach { rename-item $_ $_.Name.Replace("_123456_837P.", ".").Replace(".test.", ".sa.").Replace("_987654_837I." , ".") }
Here is the filename I want to fix
999_987654_837I.74161.test
I want to remove _987654_837I from the file name. I was just going to rename it but those numbers may change. So now I want to remove the 4th character starting at the _ and back to the "I" or 9th character.
You can use a regex pattern to get the required part.
See regex example + explanation:
https://regex101.com/r/xNoBVD/2
I use positive lookbehind to force regex to get the first 3 characters at the very beginning of the line (^) without capturing it. The following 12 characters are captured and can then be replaced with ''
$regexReplacePattern = '(?<=^.{3}).{12}'
'999_987654_837I.74161.test' -replace $regexReplacePattern, ''

Powershell - Split a string on a character

I've got a CSV file full of filepaths like below:
D:\CompanyData\REPORTS\ENQUIRIES\Old House Farm\Images
D:\CompanyData\REPORTS\ENQUIRIES\Quay House\Text
D:\CompanyData\REPORTS\ENQUIRIES\Church Road\Photography
D:\CompanyData\REPORTS\ENQUIRIES\Church Road\Reports\
D:\CompanyData\REPORTS\ENQUIRIES\Church Road\Reports\Images
I want to split them on the 5th '/' character to return the following (including the last trailing '/'
D:\CompanyData\REPORTS\ENQUIRIES\Old House Farm\
D:\CompanyData\REPORTS\ENQUIRIES\Quay House\
D:\CompanyData\REPORTS\ENQUIRIES\Church Road\
D:\CompanyData\REPORTS\ENQUIRIES\Church Road\
D:\CompanyData\REPORTS\ENQUIRIES\Church Road\
So far I've tried the following:
$source = $Item.Source.Split("\")[0]
And various other combinations of the above but can't quite get what I'm after. Can anyone assist?
Try like this:
[string](Split-Path "PATH")+"\"
If you have $Item.Source then:
[string](Split-Path "$($Item.Source)")+"\"
Here is another solution using Select-String
(Note, that the pattern uses regex, so you need to escape the backslash \ with another backslash \)
$source = ($($Item.Source) | Select-String -Pattern '.+\\.+\\.+\\.+\\.+\\' -AllMatches).Matches.Value
Output:
D:\CompanyData\REPORTS\ENQUIRIES\Old House Farm\
D:\CompanyData\REPORTS\ENQUIRIES\Quay House\
D:\CompanyData\REPORTS\ENQUIRIES\Church Road\
D:\CompanyData\REPORTS\ENQUIRIES\Church Road\Reports\
D:\CompanyData\REPORTS\ENQUIRIES\Church Road\Reports\
You can split each file path on the backslash to get an array of parts. Then join a maximum of 5 parts with a backslash and append another backslash tyo that:
$parts = "D:\CompanyData\REPORTS\ENQUIRIES\Old House Farm\Images" -split '\\'
'{0}\' -f ($parts[0..[math]::Min($parts.Count, 4)] -join '\')
Or do this with regex:
"D:\CompanyData\REPORTS\ENQUIRIES\Old House Farm\Images" -replace '^(([^\\]+\\){1,5}).*', '$1'
Regex details:
^ Assert position at the beginning of the string
( Match the regular expression below and capture its match into backreference number 1
( Match the regular expression below and capture its match into backreference number 2
[^\\] Match any character that is NOT a “A \ character”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\\ Match the character “\” literally
){1,5} Between one and 5 times, as many times as possible, giving back as needed (greedy)
)
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Result:
D:\CompanyData\REPORTS\ENQUIRIES\Old House Farm\
Simple regex version:
$Item.Source -replace "(('[^\\]+\\){1,5}).*", '$1'
[^\\]+\\ matches 1-n non-\ followed by \
{1,5} repeat the pattern 5 times

How to replace character in Powershell with initial character + a space

Hopefully this question isn't already answered on the site. I want to replace every number in a string with that number and a space. So here's what I have right now:
"31222829" -replace ("[0-9]","$0 ")
The [0-9] looks for any numbers, and replaces it with that character and the space. However, it doesn't work. I saw from another website to use $0 but I'm not sure what it means.The output I was looking for was
3 1 2 2 2 8 2 9
But it just gives me a blank line. Any suggestions?
LardPies
This probably isn't the right way to do it, but it works.
("31222829").GetEnumerator() -join " "
The .GetEnumerator method iterates over each character in the string
The -join operator will then join all of those characters with the " " space
tl;dr
PS> "31222829" -replace '[0-9]', '$& '
3 1 2 2 2 8 2 9
Note that the output string has a trailing space, given that each digit in the input ([0-9]) is replaced by itself ($&) followed by a space.
As for what you tried:
"31222829" -replace ("[0-9]","$0 ")
While enclosing the two RHS operands in (...) doesn't impede functionality, it's not really helpful to conceive of them as an array - just enumerate them with ,, don't enclose them in (...).
Generally, use '...' rather than "..." for the RHS operands (the regex to match and the replacement operand), so as to prevent confusion between PowerShell's string expansion (interpolation) and what the -replace operator ultimately sees.
Case in point: Due to use of "..." in the replacement operand, PowerShell's string interpolation would actually expand $0 as a variable up front, which in the absence of a variable expands to the empty string - that is why you ultimately saw a blank string.
Even if you had used '...', however, $0 has no special meaning in the replacement operand; instead, you must use $& to represent the matched string, as explained in this answer.
To unconditionally separate ALL characters with spaces:
Drew's helpful answer definitely works.
Here's a more PowerShell-idiomatic alternative:
PS> [char[]] '31222829' -join ' '
3 1 2 2 2 8 2 9
Casting a string to [char[]] returns its characters as an array, which -join then joins with a space as the separator.
Note: Since -join only places the specified separator (' ') between elements, the resulting string does not have a trailing space.
You can use a regex with positive lookahead to avoid the trailing space. Lookahead and lookbehind are zero-length assertions similar to ^ and $ that match the start/end of a line. The regex \d(?=.) will match a digit when followed by another character.
PS> '123' -replace '\d(?=.)', '$0 '
1 2 3
To verify there's no trailing space:
PS> "[$('123' -replace '\d(?=.)', '$0 ')]"
[1 2 3]

Extract the nth to nth characters of an string object

I have a filename and I wish to extract two portions of this and add into variables so I can compare if they are the same.
$name = FILE_20161012_054146_Import_5785_1234.xml
So I want...
$a = 5785
$b = 1234
if ($a = $b) {
# do stuff
}
I have tried to extract the 36th up to the 39th character
Select-Object {$_.Name[35,36,37,38]}
but I get
{5, 7, 8, 5}
Have considered splitting but looks messy.
There are several ways to do this. One of the most straightforward, as PetSerAl suggested is with .Substring():
$_.name.Substring(35,4)
Another way is with square braces, as you tried to do, but it gives you an array of [char] objects, not a string. You can use -join and you can use a range to make that easier:
$_.name[35..38] -join ''
For what you're doing, matching a pattern, you could also use a regular expression with capturing groups:
if ($_.name -match '_(\d{4})_(\d{4})\.xml$') {
if ($Matches[1] -eq $Matches[2]) {
# ...
}
}
This way can be very powerful, but you need to learn more about regex if you're not familiar. In this case it's looking for an underscore _ followed by 4 digits (0-9), followed by an underscore, and four more digits, followed by .xml at the end of the string. The digits are wrapped in parentheses so they are captured separately to be referenced later (in $Matches).
Yet another approach: returns 1234 substring four times.
$FileName = "FILE_20161012_054146_Import_5785_1234.xml"
# $FileName
$FileName.Substring(33,4) # Substring method (zero-based)
-join $FileName[33..36] # indexing from beginning (zero-based)
-join $FileName[-8..-5] # reverse indexing:
# e.g. $FileName[-1] returns the last character
$FileArr = $FileName.Split("_.") # Split (depends only on filename "pattern template")
$FileArr[$FileArr.Count -2] # does not depend on lengths of tokens

How do I find the sum of all numbers in STDIN even if there are non-digit characters?

I have an assignment asking me to enter a sequence of numbers and characters each separated by a space and the sequence in ended by entering in "q" or "Q" followed by a space. Everything except the numbers should be discarded and we are to find the sum. So for example if the input is "1 12 a 2 5 P Q" then we should expect to get "20" as the output.
So far I'm using
$input = <>;
$input =~ tr/0-9//cd;
to get only the numbers but what I want is to split them up and get the sum. Right now the output would be 11225 and I want "1+12+2+5" and get the sum.
perl -ne '$s=0;($line)=/(.*?)[Qq]/;while($line=~/(\d+)/g) {$s+=$1} print "$s\n"'
Explanation:
Strips the trailing part of each line starting with a Q or a q, then scan the remaining part for isolated positive integers and adds these together.
First, strip out all characters that aren't numbers or spaces:
$input =~ s/[^0-9\s]//g;
Then, split on whitespace:
#digits = split(/\s/, $input);
Then you have a list of digits that you can add up.
Preserve spaces in your first step:
$input =~ tr/0-9 //cd;
Then split on spaces:
my #numbers = split ' ', $input;
(this is a special form of split that works like split /\s+/ but also discards empty leading fields).
You probably want to start by getting rid of everything after a Q though:
$input =~ s/Q .*//i;
For what it's worth, I wouldn't have jumped to using tr here; I'd have started by spliting on spaces, then processed fields that were only digits until a Q was reached.