Regex: How to remove extra spaces between strings in Perl

前端 未结 5 443
不知归路
不知归路 2021-01-18 10:03

I am working on a program that take user input for two file names. Unfortunately, the program can easily break if the user does not follow the specified format of the input.

5条回答
  •  醉梦人生
    2021-01-18 10:43

    And here is one more way you could do it with regex (if you are reading the input from STDIN):

    # read a line from STDIN
    my $filenames = ;
    
    # parse the line with a regex or die with an error message
    my ($qseq_filename, $barcode) = $filenames =~ /^\s*(\S.*?)\s*,\s*(\S.*?)\s*$/
        or die "invalid input '$filenames'";
    

提交回复
热议问题