2017-07-27 46 views
-4

我看了类似的问题和答案,但无法解决我的问题。查找字符串之间的文本python

我有一个字符串,如下所示:

ecc, ecc, .....thisIsUnique("92781227-7e7e-4768-8ee3-4e1615bddf3c", ecc, ecc....... 

可能会很长之前和之后,无需一些独特文本。

我需要的是将92781227-7e7e-4768-8ee3-4e1615bddf3c代码作为字符串。所以,我在找的东西,CA听起来像:

当你发现 thisIsUnique先走了,阅读代码,你找到的第一个字符 (",并保持阅读,直到你找到的第一个字符 ",

不幸的是我不熟悉的正则表达式,但也许有不同的方式

由于解决问题的所有

回答

2

有几个网站你应该阅读正则表达式是什么。 https://regexone.com/Learning Regular Expressions使用这样的网站来测试你已经尝试什么:https://regex101.com/但让你开始,这个运行正是您粘贴为例:

import re 

text = 'ecc, ecc, .....thisIsUnique("92781227-7e7e-4768-8ee3-4e1615bddf3c", ecc, ecc.......' 
match = re.search('thisIsUnique\("([^"]+)', text) 
print (match.group(1)) 

结果:

92781227-7e7e-4768-8ee3-4e1615bddf3c 
+0

完美!工作很好。我一定会看看你发布的链接 – matteo

1

使用re.search

In [991]: text = 'ecc, ecc, .....thisIsUnique("92781227-7e7e-4768-8ee3-4e1615bddf3c", ecc, ecc.......' 

In [992]: re.search('(?<=thisIsUnique\(")(.*?)"', text).group(1) 
Out[992]: '92781227-7e7e-4768-8ee3-4e1615bddf3c' 

'(?<=thisIsUnique\(")(.*?)"' 

采用向后看。


读物

  1. Regex HOWTO - 入门教程

  2. General documentation

  3. 附加教程网站 - TutorialsPoint

+0

@DeepSpace我不这么认为? –

+0

非常感谢!它的作品非常好 – matteo

+0

@matteo供将来参考,我可以在哪里改进我的答案? –

相关问题