正则表达式查找逗号之间的最后一个单词

时间:2015-06-19 19:00:36

标签: regex comma between

我有这样的事情:“我的item1,item2,item3,item4,”

我只想答案:“item4”

我用这个:

    (?<=\,).*(?=\,$)

但它返回我:“item2,item3,item4”

我怎么才能得到最后一个?

我知道那些懒惰的标志? ,但我无法使用它。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

使用.*代替.,因为.*?也会匹配字符逗号。这里非贪婪的表格(?<=,)[^,]*(?=,$) 也不起作用。

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
#  Usually these files are written by a python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

答案 1 :(得分:0)

只是一个建议,你也可以使用split命令,如下所示:

x = "my item1,item2,item3,item4"
print x.split(",")[-1]

这将打印最后一个元素。