Perl print matched regex string [duplicate] - regex

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.

Related

perl regex extracted from one file to use on a variable [duplicate]

This question already has answers here:
How can I use a variable in the replacement side of the Perl substitution operator?
(9 answers)
Closed 3 years ago.
I have a file containing search and replace string in a single line.
And I am reading that file, using split to separate search and replace string
and apply it on a variable.
File:
(.*) pre_$1
Perl Code:
$str = "a";
$line = < FILEHANDLE>; # Read above file.Contains (.*) pre_$1
my ($ss,$rs) = split /\s/,$line;
$str =~ s/$ss/$rs/ee;
This seems to be not working.
I tried to look online, one result is close which is wrap the replace string in both single and double quotes.
i.e.:
$rs = '"pre_$1"';
This works if its in the script, but if I read from file I done see any replacement.
Can someone point me to what I am doing wrong here?
Thanks.
s//$rs/ee expects $rs to contains valid Perl code. pre_$1 is not valid Perl code. It's a very bad idea to expect the user to provide Perl code anyway.
Solution:
use String::Substitution qw( gsub_modify );
gsub_modify($str, $ss, $rs);

RegEx match all word characters, with umlauts from different languages [duplicate]

This question already has answers here:
Why do Perl string operations on Unicode characters add garbage to the string?
(7 answers)
Closed 3 years ago.
I want to check if a person's name is valid.
It should check latin letters, also with umlauts (i.e. öäüÖÄÜé).
unfortunately nothing i've tried works.
regarding many sources (following some links),
https://www.regular-expressions.info/unicode.html
Regex for word characters in any language
\p{L} should work, but it doesn't works for me.
Do i have to use a library for this?
use strict;
use warnings;
my $test = "testString";
print $1 if ($test =~ m/^(\p{L}+)$/); #testString
$test = "testStringö";
print $1 if ($test =~ m/^(\p{L}+)$/); #no print msg
$test = "testéString";
print $1 if ($test =~ m/^(\p{L}+)$/); #no print msg
You need to tell Perl that the source code of your file is in utf8. Add
use utf8;
After
use strict;

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.

String to split a complicated string in Perl [duplicate]

This question already has answers here:
How can I parse quoted CSV in Perl with a regex?
(7 answers)
Closed 7 years ago.
I have a string that looks something like this:
'som,ething', another, 'thin#'g', 'her,e', gggh*
I am trying to get it to split on the commas that are NOT in the elements, like this:
'som,ething'
another
'thin#'g'
'her,e'
gggh*
I am using parse_line(q{,}, 1, $string) but it seems to fail when the string has single quotes in it. Is there something I'm missing?
#!/usr/bin/perl
use strict;
use warnings;
my $string = q{'som,ething', another, 'thin'g', 'her,e', gggh*};
my #splitted = split(/,(?=\s+)/, $string);
print $_."\n" foreach #splitted;
Output:
'som,ething'
another
'thin'g'
'her,e'
gggh*
Demo
It looks like you're trying to parse comma-separated values. The answer is to use Text::CSV_XS since that handles the various weird cases you're likely to find in the data. See How can I parse quoted CSV in Perl with a regex?
Using split is not the way to go. If you are sure your string is well formatted using a global match is more simple, example:
my $line = "'som,ething', another , 'thin#'g', 'her,e' , gggh*";
my #list = $line =~ /\s*('[^#']*(?:#.[^#']*)*+'|[^,]+(?<=\S))\s*/g;
print join("|", #list);
(the (?<=\S) is only here to trim items on the right)

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