Removing delimiters from a date / time string

I want to take it

Code:
2010-12-21 20:00:00

and do this:

Code:
20101221200000

This is the last thing I tried

Code:
#!/usr/bin/perl  -w
use strict;
my ($teststring) = '2010-12-21 20:00:00';
my $result =  " ";
print "$teststring\n";
$teststring =~ "/(d\{4\})(d\{3\})(d\{3\})(d\{3\})(d\{3\})(d\{3\})/$result";
        { 
    print "$_\n";
    print "$result\n";
        print "$teststring\n";
    }

And this produced it:

Code:
nathan@debian:~/Desktop$ ./ptest
2010-12-21 20:00:00
Use of uninitialized value $_ in concatenation (.) or string at ./ptest line 8.


2010-12-21 20:00:00
nathan@debian:~/Desktop$

-Thank

+3
source share
4 answers

Firstly, here is the problem with your code:

$teststring =~ "/(d\{4\})(d\{3\})(d\{3\})(d\{3\})(d\{3\})(d\{3\})/$result";

You want to use =~with a lookup operator s///. That is, the right side should not be a simple string, but s/pattern/replacement/.

\d . , \d , , [0-9], , . [0-9]{4} 0 9 . , { }.

( ) . , , , .

, , , , ( ).

/x s///, pattern .

#!/usr/bin/perl

use strict; use warnings;

while ( <DATA> ) {
    s{
        ^
        ([0-9]{4})-
        ([0-9]{2})-
        ([0-9]{2})[ ]
        ([0-9]{2}):
        ([0-9]{2}):
        ([0-9]{2})
    }{$1$2$3$4$5$6}x;
    print;
}

__DATA__
Code:
2010-12-21 20:00:00

, , 5.10, :

#!/usr/bin/perl

use 5.010;

while ( <DATA> ) {
    s{
        ^
        ( ?<year>  [0-9]{4} ) -
        ( ?<month> [0-9]{2} ) -
        ( ?<day>   [0-9]{2} ) [ ]
        ( ?<hour>  [0-9]{2} ) :
        ( ?<min>   [0-9]{2} ) :
        ( ?<sec>   [0-9]{2} )
    }
    {
        local $";
        "@+{qw(year month day hour min sec)}"
    }ex;
    print;
}

__DATA__
Code:
2010-12-21 20:00:00
+6

([^\d] [\D]) :

$ perl -e '$_ = "2010-12-21 20:00:00"; s/[\D]//g; print $_;'
20101221200000
+5

Can't you just delete something that is not a digit?

s/[^\d]//g 

in sed format, can't remember perl.

0
source
($result = $teststring) =~ y/0-9//cd;
0
source

Source: https://habr.com/ru/post/1781451/


All Articles