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

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

Related

Removing specific words from a text string? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
So say you have a variable string like: "Report to Sam.Smith"
What's the best way for you to remove the words 'Report' and 'to' leaving only Sam.Smith using Powershell??
You have to use -replace :
$string = "Report to Sam.Smith"
$string = $string -replace "Report to ",""
$string # Output --> "Sam.Smith"
Or like this :
$string = "Report to Sam.Smith"
$string = $string.replace("Report to ","")
$string # Output --> "Sam.Smith"
But if you need to use Regex because the string's words can vary then you have to rethink the problem.
You won't be looking to erase a part of the string but to extract something out of it.
In you case, I think that you're looking for a username using a name.lastname format which is pretty easy to capture :
$string = "Report to Sam.Smith"
$string -match "\s(\w*\.\w*)"
$Matches[1] # Output --> Sam.Smith
Using -match will return True / False.
If it does return True, an array named $Matches will be created. It will contains on index 0 ($Matches[0]) the whole string that matched the regex.
Every others index greater than 0 will contains the captured text from the regex parenthesis called "capture group".
I would highly recommend using an if statement because if your regex return false, the array $Matches won't exist :
$string = "Report to Sam.Smith"
if($string -match "\s(\w*\.\w*)") {
$Matches[1] # Output --> Sam.Smith
}

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

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.

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.

Wanted clarification [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I am trying to understand some code which is already written..
I am not able to understand
my $l = $0; $l =~ s-/[^/]+/[^/]+$-/lib/perl-;
what is assigned to $0 ?
what is - (Hyphen) operator in the expression ?
whay $- is used in the expression?
Please explain the ablove 3Qs.
BEGIN {
# figure out our general library path
my $p;
my $l = $0; $l =~ s-/[^/]+/[^/]+$-/lib/perl-;
push #INC,$l;
# add some platform components, based on files
push #INC,"$l/sunos" if ( grep /solaris/,#INC );
push #INC,"$l/csw" if ( grep /csw/,#INC );
push #INC,"$l/i386" if ( grep /i386/,#INC );
push #INC,"$l/x86_64" if ( grep /x86_64/,#INC );
}
You can find various Perl special variables in perlvar. $0 is explained there.
As mentioned in perlop, the substitution operator s/REGEX/REPLACEMENT/ can be written with different delimiters, e.g. dashes. It's handy when the regex or replacement contain slashes, as is the case here. Using paired delimiters might be more readable:
s{/[^/]+/[^/]+$}{/lib/perl}
or, using /x:
s{ / [^/]+ / [^/]+ $ }{/lib/perl}x

How can I find position of matched regex of a single string in Perl? [duplicate]

This question already has answers here:
How can I find the location of a regex match in Perl?
(5 answers)
Closed 7 years ago.
Let's say my $string = "XXXXXTPXXXXTPXXXXTP";
If I want to match: $string =~ /TP/; multiple times and return the position for each, how would I do so?
I have tried$-[0], $-[1], $-[2] but I only get a position for $-[0].
EDIT:
I have also tried the global modifier //g and it still does not work.
$-[1] is the position of the text captured by the first capture. Your pattern has no captures.
By calling //g in scalar context, only the next match is found, allowing you to grab the position of that match. Simply do this until you've found all the matches.
while ($string =~ /TP/g) {
say $-[0];
}
Of course, you could just as easily store them in a variable.
my #positions;
while ($string =~ /TP/g) {
push #positions, $-[0];
}
You could try:
use feature qw(say);
use strict;
use warnings;
my $str = "XXXXXTPXXXXTPXXXXTP";
# Set position to 0 in order for \G anchor to work correctly
pos ($str) = 0;
while ( $str =~ /\G.*?TP/s) {
say ($+[0] - 2);
pos ($str) = $+[0]; # update position to end of last match
}