简体   繁体   English

如何找到每个句子的最后一个词

[英]How to find the last words of every sentences

I'm trying to find all the last words from my sentences by regular expression我正在尝试通过正则表达式从我的句子中找到所有最后一个单词

I found this one ("\b(\w+)\W*[,]$") but it only finds the last word of the last sentence.我找到了这个("\b(\w+)\W*[,]$")但它只找到最后一句话的最后一个单词。

These are my example sentences.这些是我的例句。 I'm a beginner on RegEx can anyone help please?我是 RegEx 的初学者,有人可以帮忙吗?

Yasar Osman,32,usak,Kusmak Yasar Osman,32,usak,Kusmak

Ali Sonmez,kayseri,76,Kanser Ali Sonmez,开塞利,76,Kanser

Kemal Basici,istanbul,87,Kanser Kemal Basici,伊斯坦布尔,87,Kanser

Soner Mentese,istanbul,32,Oksuruk Soner Mentese,伊斯坦布尔,32,Oksuruk

what defines a word?什么定义了一个词? let's say a word consists of an arbitrary number (but min one) of characters except for space, comma or asterisk.假设一个单词由任意数量(但最少一个)字符组成,除了空格、逗号或星号。 Then your word is [^*, ]+ .那么你的词是[^*, ]+ Now you want to find the last one.现在你想找到最后一个。

The first capturing group of ([^*, ]+)[*, ]*$ is the last word of a text snippet. ([^*, ]+)[*, ]*$的第一个捕获组是文本片段的最后一个单词。 $ stands for the very end of the string. $代表字符串的末尾。 [*, ]* stands for an arbitrary number of non-word characters after the last word. [*, ]*代表最后一个单词之后的任意数量的非单词字符。

see regex101 to see how it matches your examples请参阅regex101以了解它如何与您的示例匹配

or see this example of how you can use it in python:或查看如何在 python 中使用它的示例:

import re

example = "Yasar Osman,32,usak,Kusmak"
re_match = re.search('([^*, ]+)[*, ]*$', example)
last_word = re_match.group(1)  # 'Kusmak'

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

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