开发者

Removing quotes from string

So I thought this would just be a simple issue however I'm getting the incorrect results. Basically I am trying to remove the quotes around a string. For example I have the string "01:00" and I want 01:00, below is the code on how I thought I would be able to do this:开发者_如何转开发

$expected_start_time = $conditions =~ m/(\"[^\"])/;

Every time this runs it returns 1, so I'm guessing that it is just returning true and not actually extracting the string from the quotes. This happen no matter what is in the quotes "02:00", "02:20", "08:00", etc.


All you forgot was parens for the LHS to put the match into list context so it returns the submatch group(s). The normal way to do this is:

 ($expected_start_time) = $condition =~ /"([^"]*)"/;


It appears that you know that the first and last character are quotes. If that is the case, use

$expected_start_time = substr $conditions, 1, -1;

No need for a regexp.


The brute force way is:

$expected_start_time = $conditions;
$expected_start_time =~ s/"//g;

Note that the original regex:

m/(\"[^\"])/

would capture the opening quote and the following non-quote character. To capture the non-quote characters between double quotes, you'd need some variant on:

m/"([^"]*)"/;

This being Perl (and regexes), TMTOWTDI - There's More Than One Way Do It.


In scalar context a regex returns true if the regex matches the string. You can access the match with $1. See perlre.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜