正则表达式:如何在 Perl 中删除字符串之间的多余空格 [英] Regex: How to remove extra spaces between strings in Perl

查看:79
本文介绍了正则表达式:如何在 Perl 中删除字符串之间的多余空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个接受用户输入两个文件名的程序.不幸的是,如果用户不遵循指定的输入格式,程序很容易中断.我想编写代码来提高其对这些类型错误的弹性.看到我的代码你就明白了:

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. I want to write code that improves its resiliency against these types of errors. You'll understand when you see my code:

# Ask the user for the filename of the qseq file and barcode.txt file
print "Please enter the name of the qseq file and the barcode file separated by a comma:";
# user should enter filenames like this: sample1.qseq, barcode.txt

# remove the newline from the qseq filename
chomp ($filenames = <STDIN>);

# an empty array
my @filenames;

# remove the ',' and put the files into an array separated by spaces; indexes the files
push @filename, join(' ', split(',', $filenames))

# the qseq file
my $qseq_filename = shift @filenames;

# the barcode file.
my barcode = shift @filenames;

显然,如果用户输入错误类型的文件名(.tab 文件而不是 .txt 或 .seq 而不是 .qseq),此代码运行可能会出错.我想要可以进行某种检查的代码,以查看用户是否输入了适当的文件类型.

Obviously this code runs can run into errors if the user enters the wrong type of filename (.tab file instead of .txt or .seq instead of .qseq). I want code that can do some sort of check to see that the user enters the appropriate file type.

另一个可能会破坏代码的错误是用户在文件名前输入了太多空格.例如:sample1.qseq,(这里假设有6个空格)barcode.txt(注意逗号后面有很多空格)

Another error that could break the code is if the user enters too many spaces before the filenames. For example: sample1.qseq,(imagine 6 spaces here) barcode.txt (Notice the numerous spaces after the comma)

再举个例子:(假设这里有6个空格)sample1.qseq,barcode.txt(这次注意第一个文件名前的空格数)

Another example: (imagine 6 spaces here) sample1.qseq,barcode.txt (This time notice the number of spaces before the first filename)

我还需要可以删除多余空格的代码行,以便程序不会中断.我认为用户输入必须采用以下格式:sample1.qseq、barcode.txt.用户输入必须采用这种格式,以便我可以正确地将文件名索引到一个数组中并稍后将它们移出.

I also want lines of code that can remove extra spaces so that the program doesn't break. I think the user input has to be in the following kind of format: sample1.qseq, barcode.txt. The user input has to be in this format so that I can properly index the filenames into an array and shift them out later.

非常感谢任何帮助或建议!

Thanks any help or suggestions are greatly appreciated!

推荐答案

处理此类问题的标准方法是使用命令行选项,而不是从 STDIN 收集输入.Getopt::Long 与 Perl 一起提供并且可服务:

The standard way to deal with this kind of problem is utilising command-line options, not gathering input from STDIN. Getopt::Long comes with Perl and is servicable:

use strict; use warnings FATAL => 'all';
use Getopt::Long qw(GetOptions);
my %opt;
GetOptions(\%opt, 'qseq=s', 'barcode=s') or die;
die <<"USAGE" unless exists $opt{qseq} and $opt{qseq} =~ /^sample\d[.]qseq$/ and exists $opt{barcode} and $opt{barcode} =~ /^barcode.*\.txt$/;
Usage: $0 --qseq sample1.qseq --barcode barcode.txt
       $0 -q sample1.qseq -b barcode.txt
USAGE
printf "q==<%s> b==<%s>\n", $opt{qseq}, $opt{barcode};

shell 会处理任何多余的空白,试试看吧.您需要对文件名进行验证,我在示例中使用正则表达式编写了一些内容.使用 Pod::Usage 以更好的方式向可能获得调用错误.

The shell will deal with any extraneous whitespace, try it and see. You need to do the validation of the file names, I made up something with regex in the example. Employ Pod::Usage for a fancier way to output helpful documentation to your users who are likely to get the invocation wrong.

CPAN 上有许多更高级的 Getopt 模块.

There are dozens of more advanced Getopt modules on CPAN.

这篇关于正则表达式:如何在 Perl 中删除字符串之间的多余空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆