简体   繁体   English

如何从用空格关闭的字符串中排除子字符串?

[英]How to exclude substring from string closing with whitespace?

I would like to exclude substring starting from '>' and closing with ' '. 我想排除以'>'开头并以''结尾的子字符串。 Example: 例:

#!/usr/bin/python

content = "classTest>528 points"
substring = content['>' : ' ']
print substring     # 528

Look up regexes. 查找正则表达式。 Here's a minimal working example for your test case: 这是您的测试用例的最小工作示例:

import re

content = "classTest>528 points"
substring = re.search(r'>(\d+) ', content).group(1)
print substring

If you do not want to use regular expressions (which has a bit of a learning curve), you can use this simpler but longer code. 如果您不想使用正则表达式(有点儿学习困难),则可以使用此更简单但更长的代码。

content = "classTest>528 points"
i = content.find('>')
j = content.find(' ', i+1)
substring = content[i+1:j]
print(substring)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM