Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何使用正则表达式提取字符串_Regex_String_Perl - Fatal编程技术网

Regex 如何使用正则表达式提取字符串

Regex 如何使用正则表达式提取字符串,regex,string,perl,Regex,String,Perl,我有以下字符串:SL2.40ch12:53884872-53885197 我想将SL2.40ch12分配给$chromose,53884872分配给$start,53885197分配给$end。使用正则表达式执行此操作的有效方法是什么 下面是我怎么做的,但我的正则表达式不可用 my $string = SL2.40ch12:53884872-53885197 my $chromosome =~ /^*\:$/ my $start =~ /^+d\-$/ my $end =~ /^-+d\/ 谢

我有以下字符串:
SL2.40ch12:53884872-53885197

我想将
SL2.40ch12
分配给
$chromose
53884872
分配给
$start
53885197
分配给
$end
。使用正则表达式执行此操作的有效方法是什么

下面是我怎么做的,但我的正则表达式不可用

my $string = SL2.40ch12:53884872-53885197
my $chromosome =~ /^*\:$/
my $start =~ /^+d\-$/
my $end =~ /^-+d\/

谢谢

我对Perl不是很熟悉,但是如果它使用通用的regexp语法,$start和$chromose行就会出错。
“$”-表示行尾。因此,它将尝试在这一行的末尾找到破折号。

我对Perl不是很熟悉,但是如果它使用常见的regexp语法,那么$start和$chromose行就会出错。
“$”-表示行尾。因此,它将尝试在行的末尾找到破折号。

这里有一个正则表达式,它可以执行您想要的操作:

($chromosome, $begin, $end) = /^(.*):(.*)-(.*)$/;

下面是一个正则表达式,它可以执行您想要的操作:

($chromosome, $begin, $end) = /^(.*):(.*)-(.*)$/;

对于该特定字符串,可以执行以下简单操作:

my $string = "SL2.40ch12:53884872-53885197";
my ($chr, $start, $end) = split /[:-]/, $string, 3; 
如果你想更严格一点,就分开做

my ($chr, $range) = split /:/, $string, 2;
my ($start, $end) = split /-/, $range;

当然,这是假设数据中的其他位置不会出现冒号或破折号。

对于特定字符串,可以执行如下简单操作:

my $string = "SL2.40ch12:53884872-53885197";
my ($chr, $start, $end) = split /[:-]/, $string, 3; 
如果你想更严格一点,就分开做

my ($chr, $range) = split /:/, $string, 2;
my ($start, $end) = split /-/, $range;

当然,这是假设您的数据中不会出现冒号或破折号。

[:-]
像您的微笑:)
[:-]
像您的微笑:)