简体   繁体   中英

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.

Tweeky way!

>>> char1 = '('
>>> char2 = ')'
>>> mystr = "mystring(123234sample)"
>>> print mystr[mystr.find(char1)+1 : mystr.find(char2)]
123234sample

$ is a special character in regex (it denotes the end of the string). You need to escape it:

>>> re.findall(r'\$(.*?)\$', '$sin (x)$ is an function of x')
['sin (x)']

You need to start searching for the second character beyond start :

end = string.index(end_marker, start + 1)

because otherwise it'll find the same character at the same location again:

>>> start_marker = end_marker = '$'
>>> string = '$sin (x)$ is an function of x'
>>> start = string.index(start_marker) + len(start_marker)
>>> end = string.index(end_marker, start + 1)
>>> string[start:end]
'sin (x)'

For your regular expressions, the $ character is interpreted as an anchor, not the literal character. Escape it to match the literal $ (and look for things that are not $ instead of not " :

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

which gives:

>>> import re
>>> re.findall('\$([^$]*)\$', string)
['sin (x)']

The regular expression $()$ otherwise doesn't really match anything between the parenthesis even if you did escape the $ characters.

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

   line_word = line.split('!')
   print (line_word[1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM