Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the string between two special characters?

For example, I need everything in between the two square brackets. File1

[Home sapiens]
[Mus musculus 1]
[virus 1 [isolated from china]]

So considering the above example, I need everything in between the first and last square brackets.

like image 641
Lord Voldemort Avatar asked Feb 05 '13 20:02

Lord Voldemort


People also ask

How do I get the string between two symbols?

If you want to extract part string between two same characters, you can do as this: Select a cell which you will place the result, type this formula =SUBSTITUTE(MID(SUBSTITUTE("/" & A3&REPT(" ",6),"/",REPT(",",255)),2*255,255),",",""), and press Enter key.

How do I extract text between two delimiters?

The easiest way to extract a substring between two delimiters is to use the text to column feature in Excel, especially if you have multiple delimiters. In this example, use =MID(A2, SEARCH(“-“,A2) + 1, SEARCH(“-“,A2,SEARCH(“-“,A2)+1) – SEARCH(“-“,A2) – 1) in cell B2 and drag it to the entire data range.


1 Answers

You can use a greedy regex:

re.search(r'\[(.*)\]', your_string).group(1)
like image 174
Blender Avatar answered Oct 14 '22 04:10

Blender