Python:在//正则表达式之间返回一个字符串 [英] Python: return a string between // regex

查看:58
本文介绍了Python:在//正则表达式之间返回一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是不懂正则表达式.我阅读了文档,但是在我想出正则表达式字符之后,如何使用它们?

I still do not understand regex. I read the docs, but after I came up with the regex character, then how to use them?

例如,我只想返回前两个斜杠之间的任何内容

For example, I want to return only whatever between the first two slashes

en/lemon_peel/n/,
ca/llimona/n/,

is 的输出应该是:lemon_peel 和 llimona

the output for is should be: lemon_peel and llimona

我在正则表达式测试器上试过这样的东西:([^/]*) 但从来没有工作过

I tried on regex tester something like this: ([^/]*) but never worked

推荐答案

如果你想使用正则表达式,你应该像这样使用正则表达式:

If you want to use regex you should use regex like this:

import re

tmp_string = '''
    en/lemon_peel/n/,
    ca/llimona/n/,
    '''

reg = re.search('/([^/,]*)/',tmp_string)
result = reg.group(1)

在您的情况下,您会找到所有不是/"的符号.

In your case you find all of symbols that are not '/'.

或者你可以使用split函数代替re:

Or you can use split function instead of re:

tmp_string.split('/')[1]

这篇关于Python:在//正则表达式之间返回一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆