0

I have the following string: SL2.40ch12:53884872-53885197.

I would like to assign SL2.40ch12 to $chromosome, 53884872 to $start and 53885197 to $end. What's an efficient way using regular expression for doing this?

Here's how I tried doing it but my regex is off.

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

thanks

0

3 Answers 3

2

For that particular string, you can do something simple like this:

my $string = "SL2.40ch12:53884872-53885197";
my ($chr, $start, $end) = split /[:-]/, $string, 3; 

If you want it a little stricter, do them separately

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

This is, of course, assuming that you will not have colons or dashes appearing elsewhere in your data.

Sign up to request clarification or add additional context in comments.

1 Comment

[:-] like your smiles :)
1

Here is a regex that may do what you want:

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

Comments

0

I'm not really familiar with Perl, but if it uses common regexp syntax than your $start and $chromosome lines have an mistake. '$' - means end-of-the-line. So it will try to find dash at the end of the line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.