Marshall has asked for the wisdom of the Perl Monks concerning the following question:
In case2, there is a new line right after Club:. My attempts at a single regex often wind up capturing Comments: instead of a null string for Club name. Ideas welcome.
Update:#!/usr/bin/perl use strict; use warnings; my $case1 = "Club: Some Club Comments: "; my $case2 = "Club: Comments: "; foreach my $text ($case1, $case2) { # extract Club name (my $club) = $text =~ /Club:(.*)/; # need to allow for blank Club + name $club =~ s/^\s*//; $club =~ s/\s*$//; print "line before name\n"; # print out to make sure $club doesn' +t have a \n print "extracted Club name: '$club'\n"; print "line after name\n"; } __END__ line before name extracted Club name: 'Some Club' line after name line before name extracted Club name: '' line after name ======== I've tried things like: /Club:\s*(.*)[ ]*\n/ and various other encantations, but that picks up 'Comments:' intead of a blank Club name line before name extracted Club name: 'Some Club' line after name line before name extracted Club name: 'Comments:' line after name
#!/usr/bin/perl use strict; use warnings; my $case1 = "Club: Some Club Comments: "; my $case2 = "Club: Comments: "; foreach my $text ($case1, $case2) { # extract Club name (my $club) = $text =~ /Club:\h*(.*?)\h*\n/; # need to allow for b +lank Club name print "line before name\n"; # print out to make sure $club doesn' +t have a \n print "extracted Club name: '$club'\n"; print "line after name\n"; } __END__ line before name extracted Club name: 'Some Club' line after name line before name extracted Club name: '' line after name
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Extracting sometimes null string from text variable
by LanX (Saint) on Sep 17, 2017 at 18:41 UTC | |
by Anonymous Monk on Sep 17, 2017 at 18:47 UTC | |
by LanX (Saint) on Sep 17, 2017 at 19:07 UTC | |
|
Re: (Solved) Extracting sometimes null string from text variable
by Anonymous Monk on Sep 17, 2017 at 21:09 UTC | |
by Marshall (Canon) on Sep 18, 2017 at 01:21 UTC | |
by RonW (Parson) on Sep 21, 2017 at 22:23 UTC | |
by jdporter (Paladin) on Sep 26, 2017 at 15:55 UTC |