This question already has answers here:
Regular expression matching in PowerShell
(5 answers)
Closed 6 years ago.
Given a string:
$assemblyVersion = '[assembly: AssemblyFileVersion("8.0.0.866")]'
I have a regex that will find the assembly numbers:
(\d+\.)(\d+\.)(\d+\.)(\*|\d+)
These lines confirms it has found a match:
$extractedVersion = $assemblyVersion -match "(\d+\.)(\d+\.)(\d+\.)(\*|\d+)";
How do you then extract the match from the original string $assemblyVersion?
Reading this article i don't quite get how you do it...
I tried:
$assemblyVersion
$extractedVersion = $assemblyVersion -match "?<assembly>(\d+\.)(\d+\.)(\d+\.)(\*|\d+)";
Write-Host "Extracted " $extractedVersion["assembly"]
Doesn't work.
My regex may not be optimal either.
This stackoverflow answer doesn't help
I got it:
$assemblyVersion = '[assembly: AssemblyFileVersion("8.0.0.866")]'
$assemblyVersion -match "(\d+\.)(\d+\.)(\d+\.)(\*|\d+)";
Write-Host "Extracted " $matches[0]
Related
This question already has answers here:
Compare a string in one array, with a wildcard string in another
(3 answers)
Powershell text search - multiple matches
(2 answers)
-contains or -match several values
(2 answers)
Closed 19 days ago.
Code:
$regex = #(
'foo'
'bar'
'baz'
)
Get-Content .\foo.data | Where-Object {$_ -match $regex} | ForEach-Object {
write-host $_
}
Which was inspired by this text processing question.
The context is that I'm looking to clean up a noisy file, so lines which don't nicely fit what ConvertFrom-StringData or ConvertFrom-StringData can handle are omitted.
Only lines which match from the $regex whitelist should be output.
Looking at grep the question is seemingly: how to use and/or pattern matching in the above sample. For example:
nicholas#mordor:~/powershell$
nicholas#mordor:~/powershell$ grep -e "foo" -e "bar" foo.data
but with Powershell, and only processing a single line at a time. Effectively, running a switch to reject what's not white-listed.
This question already has answers here:
Why are all newlines gone after PowerShell's Get-Content, Regex, and Set-Content?
(4 answers)
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 2 years ago.
The goal is simple:
Take contents of text file
Search for pattern
Save search back to the file
For example, i want to find the first occurrence between # and ##. Following regex works perfectly (\#)(.*?)(?=\#{2}). It finds what I want. However, PowerShell removes all new line characters effectively changing the formatting. So, following input text
#
This
Is
My
Text
##
becomes this
# This Is My Text
How to preserve the formatting?
Here is my PowerShell script
param (
[string]$filename
)
$content = Get-Content -Path $filename
$output = $filename
$regex = [Regex]::new('(\#)(.*?)(?=\#{2})')
$matches = $regex.Matches($content)
Set-Content -Path $output $Matches[0]
This question already has answers here:
Is there a way to make Perl regex searches case-insensitive?
(2 answers)
Closed 4 years ago.
Is there a minimal adjustment that i can do to the below code to cover cases where the ManagedElement String is of any case?
if($cmd =~ /^ManagedElement/){
$cmd = "TreeNavigation";
}
Yes, put an i at the end.
if($cmd =~ /^ManagedElement/i){
$cmd = "TreeNavigation";
}
Here is the documentation for future reference.
This question already has answers here:
How can I extract the matches from the Perl match operator into variables?
(6 answers)
Closed 5 years ago.
The following perl code only gives back true or false (1 & 0)
#!/usr/bin/perl
use strict;
use warnings;
my $string;
$string ="interface Ethernet1/20
shutdown";
my $test = $string =~ m/^.+$(?=\s+shutdown)/mg;
print "'$test'\n";
I get back a 1.
But how can I get back the matched string 'interface Ethernet1/20' ?
Thanks for every help!
Simply give it list context:
my ($test) = $string =~ m/^.+$(?=\s+shutdown)/mg;
The concept of evaluation context (list vs scalar) is fundamental to Perl programming, so it may be time to review some tutorials and/or a reference manual.
This question already has answers here:
How to handle backslash character in PowerShell -replace string operations?
(2 answers)
Closed 6 years ago.
Folks
I am unable to replace the below string in PowerShell, initially I suspected it to be due to regexp and used [regex]::Escape() to resolve the \ to double slash however it still does not work. Can you please suggest?
PS C:\User>"C:clog" -replace "C:c" , ""
PS C:\User>log
PS C:\User>"C:\\c\\log" -replace "C:\\c\\", ""
PS C:\User>C:\\c\\log
Use Split-Path, it is designed for this:
Split-Path "C:\\c\\log" -Leaf
# returns "log"