Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Matching dot using regexp

by Anonymous Monk
on Oct 09, 2017 at 18:09 UTC ( [id://1201027]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to match the dot using the following regular expression

#!/volume/perl/bin/perl use warnings; use strict; use DateTime::Duration; my $tim = "01:00:01.004"; $tim =~ s/:/./g; print "new time is $tim"; my @outputlist = split /./, $tim; print "$outputlist[0]\n";

It shows the following error . let me know how to split using dot

Use of uninitialized value in concatenation (.) or string at ./temp-ip2.pl line 11. new time is 01.00.01.004

Replies are listed 'Best First'.
Re: Matching dot using regexp
by Discipulus (Canon) on Oct 09, 2017 at 18:14 UTC
    The dot has special meaning in regexes. you must escape it:  /\./

    See perlrequick for character that must be escaped:

    Not all characters can be used as is in a match. Some characters, called metacharacters, are reserved for use in regex notation. The metacharacters are

    {}[]()^$.|*+?\

    Then if you add a newline to your first print statement print "new time is $tim\n"; or use say the whole output will be clearer.

    update please note that you are splitting on any character, resulting in an empty list:

    perl -MData::Dump -e "dd split /./, $ARGV[0]" assasas ()

    Note also that you can split on an alternation /\.|:/ , avoiding the first regex:

    perl -MData::Dump -e "dd split /\.|:/, $ARGV[0]" 10:A.11:B.12 (10, "A", 11, "B", 12)
    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Matching dot using regexp
by LanX (Saint) on Oct 09, 2017 at 18:12 UTC
Re: Matching dot using regexp
by huck (Prior) on Oct 09, 2017 at 18:46 UTC

    I had to think for a bit as to why it was undefined rather than ''

    split notes "If LIMIT is omitted (or, equivalently, zero), then it is usually treated as if it were instead negative but with the exception that trailing empty fields are stripped (empty leading fields are always preserved);"

    use warnings; use strict; my $t0="01.00.01.004"; my @o0 = split /./, $t0; my @om1 = split /./, $t0,-1; use Data::Dumper; print " 0\n".Dumper(\@o0); print "-1\n".Dumper(\@om1);
    Result
    0 $VAR1 = []; -1 $VAR1 = [ '', '', '', '', '', '', '', '', '', '', '', '', '' ];

    Without the -1 any char is a delimiter, and there are no "fields" between delimiters, so all are stripped. With the -1, while all are delimiters and there are no "fields" between delimiters, each delimiter returns an empty field.

Re: Matching dot using regexp
by Laurent_R (Canon) on Oct 09, 2017 at 18:42 UTC
    TIMTOWTDI. If you use a character class for your regex, then you don't need to escape the dot:
    my @outputlist = split /[.:]/, $tim; # 01 00 01 004
Re: Matching dot using regexp
by Marshall (Canon) on Oct 09, 2017 at 18:27 UTC
    Another way to do this is with "match global". In this case, we say essentially "give me the sequences of digits found". What separates the digit sequences (i.e. numbers) doesn't matter. non-digit stuff gets skipped. This is a case of specifying what we want instead of specifying and splitting upon what we don't want.
    #!/volume/perl/bin/perl use warnings; use strict; use DateTime::Duration; my $time = "01:00:01.004"; my @output_list = $time =~ /(\d+)/g; print "@output_list\n"; # 01 00 01 004
    Update: Also, often in Perl, it is advantageous to assign meaningful names to components of a split or match global. It is rare to see something like $output_list[3] in my Perl code because the reader has to know what the heck the value at index 3 means.
    #!/volume/perl/bin/perl use warnings; use strict; use DateTime::Duration; my $time = "01:00:01.004"; my ($hour,$min,$sec,$ms) = $time =~ /(\d+)/g; print "hour=$hour\n", "minutes=$min\n", "seconds=$sec\n", "milliseconds=$ms\n"; __END__ hour=01 minutes=00 seconds=01 milliseconds=004
    Update again:
    another possibility if the intent is to "delete the milliseconds"
    my $time = "01:00:01.004"; my ($no_ms) = $time =~ /([\d:]+)/; #any digit or colon print "no milliseconds = $no_ms\n"; no milliseconds = 01:00:01
    There are many variations upon this theme. I think the above is more clear, but...
    my $time = "01:00:01.004"; $time =~ s/\.\d+$//; #explictly delete the ending milli_seconds print "time stripped ms = $time\n"; #time stripped ms = 01:00:01
Re: Matching dot using regexp
by Anonymous Monk on Oct 10, 2017 at 07:35 UTC

    I am trying to match the dot using the following regular expression

    please meditate on the meaning of the words "regular expression" (its not "string literal")

      <pedantic>
      The regular expression that follows in the OP is  /./ in the split expression. And FWIW, it could, indeed, be a string literal:

      c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $tim = '01:00:01.004'; ;; $tim =~ s/:/./g; print qq{new time is '$tim'}; my @outputlist = split '\.', $tim; dd \@outputlist; " new time is '01.00.01.004' ["01", "00", "01", "004"]
      </pedantic>


      Give a man a fish:  <%-{-{-{-<

        If it was a string literal why do you need to escape the dot?

        I read thru split couldn't find a a mention of strings as separators.

        From my knowledge using a delimiter in single quotes is still a regex, albeit without variable interpolation like in double quotes.

        UPDATE

        After reading the dialog it seems that what you meant, a literal string without interpolation used as regex.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

        Heh slow days and i draws the ijits. Regular expressions are not english or chinese they're regex. No matter qr// or qr'' or qr"" whats inside is a different language it aint a plain string

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1201027]
Approved by Discipulus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-20 08:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found