如何在字符串中获取'\id '
后的第一个单词?' id'后抓住字符串中的第一个单词
字符串:
'\id hello some random text that can be anything'
蟒蛇
什么我得到
book = 'hello some random text that can be anything'
我想要什么
book = 'hello'
如何在字符串中获取'\id '
后的第一个单词?' id'后抓住字符串中的第一个单词
字符串:
'\id hello some random text that can be anything'
蟒蛇
什么我得到
book = 'hello some random text that can be anything'
我想要什么
book = 'hello'
>>> 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)
一个选项:
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
我会通过将除成类似建议一个字一个默认'除了(ValueError,IndexError):word =''' – 2012-07-13 14:31:43
@xhainingx:我不知道OP想用不同的错误条件做什么,所以我只是指出他们 – 2012-07-13 14:40:02
是的,我没有纠正你,只是建议一种可能的方法来处理它,因为这看起来不像你从一个精通python的人那里看到的那种问题 – 2012-07-13 15:22:06
在字符串书上尝试使用str.split(' ')
,该字符串将在空格上拆分并给出单词列表。然后就做book = newList[0]
。
所以book = book.split(' ')[0]
如果没有必须"\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]
既然你已经检查了符合"\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]
这个作品完美,将标记为完整:) – user1442957 2012-07-13 14:32:10
@jamylak - 显然我们在思考相同的路线。我建议你将正则表达式更改为'r'\\ id \ s *(\ w +)''以捕获多个(或不包含)空格。 – mgilson 2012-07-13 14:36:20
@mgilson OP说它像这样工作,但无论如何这是你的解决方案。虽然今天我的选票没有了,但我会加倍努力。 – jamylak 2012-07-13 14:38:05