From f772911c6a669c2f7384d12fb5ec13f57262be25 Mon Sep 17 00:00:00 2001 From: Adrian <59370927+adrianfusco@users.noreply.github.com> Date: Fri, 19 Nov 2021 21:36:42 +0100 Subject: [PATCH] Feature/new perl regex questions and answers (#186) * New questions and spell check (#181) Added new questions related with KVM, Libvirt and DNF * Add new perl regex questions and answers * Add one way to avoid one-liners in regex --- README.md | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5987237..aa8b8d5 100644 --- a/README.md +++ b/README.md @@ -5279,7 +5279,82 @@ sub factorial { ### Perl Regex
-How do you perform regular expresions in Perl?
+Check if the word `electroencefalografista` exists in a string
+ +``` +my $string = "The longest accepted word by RAE is: electroencefalografista"; +if ($string =~ /electroencefalografista/) { + print "Match!"; +} +``` +
+ +
+Check if the word `electroencefalografista` does not exists in a string
+ +``` +my $string = "The longest not accepted word by RAE is: Ciclopentanoperhidrofenantreno"; +if ($string !~ /electroencefalografista/) { + print "Does not match!"; +} +``` +
+ + +
+Replace the word `amazing`
+ +``` +my $string = "Perl is amazing!"; +$string =~ s/amazing/incredible/; +print $string; +# Perl is incredible! +``` +
+ +
+Extract `hh:mm:ss` with capturing group `()` in the following datetime
+ +``` +my $date = "Fri Nov 19 20:09:37 CET 2021"; +my @matches = $date =~ /(.*)(\d{2}:\d{2}:\d{2})(.*)/; +print $matches[1]; +# Output: 20:09:37 +``` +
+ +
+Extract all the elements that are numbers in an array
+ +``` +my @array = ('a', 1, 'b', 2, 'c', 3); +my @numbers = grep (/\d/, @array); # Note: \d involves more digits than 0-9 +map {print $_ . "\n" } @numbers; +``` + +
+ +
+Print all the linux system users that starts with d or D
+ +- With a Perl one liner :D +``` +open(my $fh, '<', '/etc/passwd'); +my @user_info = <$fh>; +map { print $& . "\n" if $_ =~ /^d([^:]*)/ } @user_info; +close $fh; +``` + +- Avoiding one-liners + +``` +foreach my $user_line (@user_info) { + if ($user_line =~ /^d([^:]*)/) { + print $& . "\n"; + } +} +``` +
### Perl Files Handle @@ -5303,7 +5378,7 @@ sub factorial { # We can use: # '>' Write (it clears a previous content if exists). # '>>' Append. -open(my $fh, '>>', 'file_name.ext') or "Error: file can't be opened"; +open(my $fh, '>>', 'file_name.ext') or die "Error: file can't be opened"; print $fh "writing text...\n"; close($fh); ``` @@ -5313,7 +5388,7 @@ close($fh); How can you read a file and print every line?
``` -open(my $fh, '<', 'file_to_read.ext') or "Error: file can't be opened"; +open(my $fh, '<', 'file_to_read.ext') or die "Error: file can't be opened"; my @file = <$fh>; foreach my $line (@file) { print $line; @@ -5323,7 +5398,7 @@ foreach my $line (@file) { We can use the file handle without assigning it to an array: ``` -open(my $fh, '<', 'file_to_read.ext') or "Error: file can't be opened"; +open(my $fh, '<', 'file_to_read.ext') or die "Error: file can't be opened"; foreach my $line (<$fh>) { print $line;