Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing quotes from string

Tags:

string

perl

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.

like image 664
Brandon Avatar asked Apr 21 '11 14:04

Brandon


People also ask

How do I remove all quotes from a string in Python?

Using the strip() Function to Remove Double Quotes from String in Python. We use the strip() function in Python to delete characters from the start or end of the string. We can use this method to remove the quotes if they exist at the start or end of the string.

How do you remove quotes from a string in C++?

Removing double quotes from string in C++ We can do this by using erase() function. Basically, what is the work of erase() function? It removes the elements from a vector or a string in some range.

How do you ignore quotes in a string?

To escape a single or double quote in a string, use a backslash \ character before each single or double quote in the contents of the string, e.g. 'that\'s it' . Copied!


2 Answers

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.

like image 125
Christoffer Hammarström Avatar answered Nov 15 '22 09:11

Christoffer Hammarström


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 =~ /"([^"]*)"/;
like image 27
tchrist Avatar answered Nov 15 '22 07:11

tchrist