Python “我怎么能?”;除名;带正则表达式的单词?

Python “我怎么能?”;除名;带正则表达式的单词?,python,regex,Python,Regex,我有一个句子,其中每个标记都有一个/。我只想在斜线之前打印出我的数据 我现在拥有的是基本的: text = less/RBR..... return re.findall(r'\b(\S+)\b', text) 很明显,这只是打印文本,如何切掉/前面的单词?简单明了: rx = r'^[^/]+' # anchor it to the beginning # the class says: match everything not a forward slash as many times a

我有一个句子,其中每个标记都有一个
/
。我只想在斜线之前打印出我的数据

我现在拥有的是基本的:

text = less/RBR.....
return re.findall(r'\b(\S+)\b', text)

很明显,这只是打印文本,如何切掉
/
前面的单词?

简单明了:

rx = r'^[^/]+'
# anchor it to the beginning
# the class says: match everything not a forward slash as many times as possible
Python
中,这将是:

import re
text = "less/RBR....."
print re.match(r'[^/]+', text)
由于这是一个对象,您可能希望将其打印出来,如下所示:

print re.match(r'[^/]+', text).group(0)
# less
这也应该起作用

\b([^\s/]+)(?=/)\b
Python代码


假设您希望删除包含斜杠的每个单词中斜杠前的所有字符。这意味着,例如,对于输入字符串
match/This,但这里只有另一个/一个
,您需要结果
match
另一个


使用正则表达式: 没有正则表达式:
所以,字符串中有多个“单词”,类似于nltk输出,对吗?应该有帮助。所以你想要的是每一个包含斜杠的单词,斜杠前的所有字符。这将用于输入字符串
match/this,但这里没有,只有另一个/一个
,您希望
match
另一个
p = re.compile(r'\b([^\s/]+)(?=/)\b')
test_str = "less/RBR/...."

print(re.findall(p, test_str))
import re
result = re.findall(r"\b(\w*?)/\w*?\b", my_string)
print(result)
result = [word.split("/")[0] for word in my_string.split()]
print(result)