17

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.

4 Answers 4

30

Tweeky way!

>>> char1 = '('
>>> char2 = ')'
>>> mystr = "mystring(123234sample)"
>>> print mystr[mystr.find(char1)+1 : mystr.find(char2)]
123234sample
Sign up to request clarification or add additional context in comments.

2 Comments

Great solution! I incorporated it as a lambda for easy re-use: GetValue = lambda mystr: mystr[mystr.find(char1) + 1 : mystr.find(char2)]
This demonstrates a good base solution but does not cover potential false-positives. If the string is not validated before hand you get bad results. my)string(123sample) or mystring(123sample\ would return strings that are likely not what you want. Using str.index() in this case would flag corrupt data (i.e. "mystring(example)")
10

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

2 Comments

what does r stands for do we need it?
raw string, so that it treats `` as an escape character
7

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

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.