bellere
bellere

Reputation: 185

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.

Upvotes: 16

Views: 66238

Answers (4)

Raja Govindan
Raja Govindan

Reputation: 195

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])

Upvotes: 7

Soorej P
Soorej P

Reputation: 531

Tweeky way!

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

Upvotes: 29

Martijn Pieters
Martijn Pieters

Reputation: 1122152

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.

Upvotes: 4

Blender
Blender

Reputation: 298196

$ 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)']

Upvotes: 10

Related Questions