Perl: Using =~ for case insensitve regex in if statement [duplicate] - regex

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.

Related

Escape character for caret(^) in Powershell [duplicate]

This question already has an answer here:
powershell: how to escape all regex characters from a string
(1 answer)
Closed 4 years ago.
I need to replace ^~^ with a tab (\t) in a CSV file in PowerShell.
I tried escaping caret with a backtick (`^~`^) but it doesn't work. How do I do this?
$file=Import-Csv $dataFilePath
$file -replace "\`^~\`^", "\`t"
Is that what you're looking for?
>$s = "^~^"
>$s -replace "\^\~\^", "``t"
`t
Hope that helps.

Perl print matched regex string [duplicate]

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.

Powershell Substring replacement [duplicate]

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"

Powershell Regex matches as string [duplicate]

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]

How can I make part of a Perl regular expression optional? [duplicate]

This question already has answers here:
How can I make part of regex optional?
(2 answers)
Closed 4 months ago.
I want match
my #array = ( 'Tree' , 'JoeTree');
foreach (#array ) {
if ( $_ =~ /^(Joe)Tree/gi) {
print "matched $_";
}
}
It matching only JoeTree. It's not matching Tree ?
Try:
if (/^(?:Joe)?Tree/gi)
We've made the Joe part optional.
Also you can change (..) to
(?:...) as you are just grouping.
Also $_ =~ part is redundant as by
default we check in $_
You missed a ?: /^(Joe)?Tree/gi