Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting string between 2 characters in python

I need to get certain words out from a string in to a new format. For example, I call the function with the input:

text2function('$sin (x)$ is an function of x')

and I need to put them into a StringFunction:

StringFunction(function, independent_variables=[vari])

where I need to get just 'sin (x)' for function and 'x' for vari. So it would look like this finally:

StringFunction('sin (x)', independent_variables=['x']

problem is, I can't seem to obtain function and vari. I have tried:

start = string.index(start_marker) + len(start_marker)
end = string.index(end_marker, start)
return string[start:end]

and

r = re.compile('$()$')
m = r.search(string)
if m:
     lyrics = m.group(1)

and

send = re.findall('$([^"]*)$',string)

all seems to seems to give me nothing. Am I doing something wrong? All help is appreciated. Thanks.

like image 687
bellere Avatar asked Feb 23 '13 17:02

bellere


People also ask

How do you take a string between two characters in Python?

The Python standard library comes with a function for splitting strings: the split() function. This function can be used to split strings between characters. The split() function takes two parameters. The first is called the separator and it determines which character is used to split the string.

How do I extract a string between two characters?

Extract part string between two different characters with formulas. To extract part string between two different characters, you can do as this: Select a cell which you will place the result, type this formula =MID(LEFT(A1,FIND(">",A1)-1),FIND("<",A1)+1,LEN(A1)), and press Enter key.

How do you get a string between two brackets in Python?

The simplest way to extract the string between two parentheses is to use slicing and string. find() . First, find the indices of the first occurrences of the opening and closing parentheses. Second, use them as slice indices to get the substring between those indices like so: s[s.


1 Answers

If you want to cut a string between two identical characters (i.e, !234567890!) you can use

   line_word = line.split('!')
   print (line_word[1])
like image 86
Raja Govindan Avatar answered Sep 28 '22 08:09

Raja Govindan