2012-07-13 55 views
1

如何在字符串中获取'\id '后的第一个单词?' id'后抓住字符串中的第一个单词

字符串:

'\id hello some random text that can be anything' 

蟒蛇

​​

什么我得到

book = 'hello some random text that can be anything' 

我想要什么

book = 'hello' 

回答

10
>>> import re 
>>> text = '\id hello some random text that can be anything' 
>>> match = re.search(r'\\id (\w+)', text) 
>>> if match: 
     print match.group(1) 

更完整版它捕获任何空白后'\id'

re.search(r'\\id\s*(\w+)', text) 
+0

这个作品完美,将标记为完整:) – user1442957 2012-07-13 14:32:10

+0

@jamylak - 显然我们在思考相同的路线。我建议你将正则表达式更改为'r'\\ id \ s *(\ w +)''以捕获多个(或不包含)空格。 – mgilson 2012-07-13 14:36:20

+0

@mgilson OP说它像这样工作,但无论如何这是你的解决方案。虽然今天我的选票没有了,但我会加倍努力。 – jamylak 2012-07-13 14:38:05

11

一个选项:

words = line.split() 
try: 
    word = words[words.index("\id") + 1] 
except ValueError: 
    pass # no whitespace-delimited "\id" in the string 
except IndexError: 
    pass # "\id" at the end of the string 
+0

我会通过将除成类似建议一个字一个默认'除了(ValueError,IndexError):word =''' – 2012-07-13 14:31:43

+3

@xhainingx:我不知道OP想用不同的错误条件做什么,所以我只是指出他们 – 2012-07-13 14:40:02

+0

是的,我没有纠正你,只是建议一种可能的方法来处理它,因为这看起来不像你从一个精通python的人那里看到的那种问题 – 2012-07-13 15:22:06

1

你不需要正则表达式这一点,你可以这样做:

book.split(' ')[0] 

但也有吨的方式来实现这一

+0

你没有读完整个问题。 – Buttons840 2012-07-13 14:50:10

0

在字符串书上尝试使用str.split(' '),该字符串将在空格上拆分并给出单词列表。然后就做book = newList[0]

所以book = book.split(' ')[0]

1

如果没有必须"\id"与字之间的空间,正则表达式会做得很好。 (如果空间有保证,然后用分液):

import re 
match=re.search(r'\\id\s*(\w+)',yourstring) 
if match: 
    print match.group(1) 

或者另一种方式(无正则表达式):

head,sep,tail=yourstring.partition(r'\id') 
first_word=tail.split()[1] 
+0

如果只有一个'id',你应该使用'str.partition'而不是 – jamylak 2012-07-13 14:51:35

+0

@jamylak - 改变。是否有理由推动分区而不是“split”?我认为它有助于解包,因为你确切地知道你会得到什么,但是'.split('\ id',1)'也可以这么说。分区速度更快吗? – mgilson 2012-07-13 14:57:52

+0

是的,它更快。 – jamylak 2012-07-13 15:00:10

0

既然你已经检查了符合"\id "开始,就分割字符串你会得到一个单词列表。如果你想下单,只得到元素#1:

>>> line="\id hello some random text that can be anything" 
>>> line.split() 
['\\id', 'hello', 'some', 'random', 'text', 'that', 'can', 'be', 'anything'] 
    #0  #1 ... 

这样,你的代码应该变成这样:

for line in lines_in: 
    if line.startswith('\id '): 
     book = line.split()[1] 
相关问题