在python中获取2个字符之间的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15043326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:10:45  来源:igfitidea点击:

getting string between 2 characters in python

pythonstringfunctioncharacterextraction

提问by bellere

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 中:

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:

我只需要为函数获取 'sin (x)',而为 vari 获取'x'。所以它最终看起来像这样:

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.

一切似乎都没有给我什么。难道我做错了什么?感谢所有帮助。谢谢。

回答by Blender

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

回答by Martijn Pieters

You need to start searching for the second character beyondstart:

您需要开始搜索超出的第二个字符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.

$()$否则,即使您确实对$字符进行了转义,正则表达式也不会真正匹配括号之间的任何内容。

回答by Soorej P

Tweeky way!

周到的方式!

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

回答by Raja Govindan

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

如果你想在两个相同的字符(即 !234567890!)之间剪切一个字符串,你可以使用

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