简体   繁体   English

查找字符串是否以相同的单词开头和结尾

[英]find whether the string starts and ends with the same word

I am trying to check whether the string starts and ends with the same word. 我试图检查字符串是否以相同的单词开头和结尾。 eg earth . 例如earth

s=raw_input();
m=re.search(r"^(earth).*(earth)$",s)
if m is not None:
    print "found"

my problem is when the string consists only of one word eg: earth 我的问题是当字符串只包含一个单词时,例如: earth

At present I have hard coded this case by 目前我已经硬编码了这个案例

if m is not None or s=='earth':
    print "found"

Is there any other way to do this? 有没有其他方法可以做到这一点?

EDIT: 编辑:

words in a string are separated by spaces. 字符串中的单词用空格分隔。 looking for a regex solution 寻找正则表达式解决方案

some examples : some examples

"earth is earth" ,"earth", --> valid “地球是地球”,“地球”, - > valid

"earthearth", "eartheeearth", "earth earth mars" --> invalid “earthearth”,“eartheeearth”,“earth earth mars” - > invalid

Use the str.startswith and str.endswith methods instead. 请改用str.startswithstr.endswith方法。

>>> 'earth'.startswith('earth')
True
>>> 'earth'.endswith('earth')
True

You can simply combine them into a single function: 您可以简单地将它们组合成一个函数:

def startsandendswith(main_str):
    return main_str.startswith(check_str) and main_str.endswith(check_str)

And now we can call it: 现在我们可以称之为:

>>> startsandendswith('earth', 'earth')
True

If, however, if the code matches words and not part of a word, it might be simpler to split the string, and then check if the first and last word are the string you want to check for: 但是,如果代码匹配单词而不是单词的一部分,则拆分字符串可能更简单,然后检查第一个和最后一个单词是否是您要检查的字符串:

def startsandendswith(main_str, check_str):
    if not main_str:  # guard against empty strings
        return False
    words = main_str.split(' ')  # use main_str.split() to split on any whitespace
    return words[0] == words[-1] == check_str

Running it: 运行它:

>>> startsandendswith('earth', 'earth')
True
>>> startsandendswith('earth is earth', 'earth')
True
>>> startsandendswith('earthis earth', 'earth')
False

You can use backreference within regex 您可以在正则表达式中使用反向引用

^(\w+\b)(.*\b\1$|$)

This would match a string only if it 这只会匹配一个字符串

  • starts and ends with the same word 相同的单词开头和结尾
  • has a single word 只有一个字

You can use str.startswith and str.endswith : 您可以使用str.startswithstr.endswith

>>> strs = "earthfooearth"
>>> strs.startswith('earth') and strs.endswith("earth")
True
>>> strs = "earth"
>>> strs.startswith('earth') and strs.endswith("earth")
True

Update: 更新:

If the words are separated by spaces and the start and end string is not known then use str.split and str.rsplit : 如果单词用空格分隔并且开始和结束字符串未知,则使用str.splitstr.rsplit

>>> strs = "foo bar foo"
>>> strs.split(None, 1)[0] == strs.rsplit(None, 1)[-1]
True
# single word
>>> strs = "foo"
>>> strs.split(None, 1)[0] == strs.rsplit(None, 1)[-1]
True
>>> strs = "foo bar ffoo"
>>> strs.split(None, 1)[0] == strs.rsplit(None, 1)[-1]
False

Here: 这里:

X = words.split()
X[:1] == X[-1:]

The slicing makes it work for empty strings too, and extend nicely to any number of words. 切片使它也适用于空字符串,并可以很好地扩展到任意数量的单词。 If words cannot be empty, use 如果words不能为空,请使用

X[0] == X[-1]

Well, if you absolutely want regex, you can make use of lookarounds, since they don't consume characters. 好吧,如果你绝对想要正则表达式,你可以使用lookarounds,因为它们不消耗字符。

>>>import re
>>>s1 = 'earth is earth'
>>>s2 = 'earth'
>>>m = re.search(r"^(?=(earth)).*(earth)$",s1)
>>>m.group(1)
'earth'
>>>m.group(2)
'earth'
>>>m = re.search(r"^(?=(earth)).*(earth)$",s2)
>>>m.group(1)
'earth'
>>>m.group(2)
'earth'

For any string, you could perhaps use this: 对于任何字符串,您可以使用此:

^(?=([A-Za-z]+)).*(\1)$

I'm assuming words as being only alphabet characters. 我假设单词只是字母字符。 If you mean words as in non-space characters, then you may go for \\S instead of [A-Za-z] . 如果你的意思是非空格字符,那么你可以去\\S而不是[A-Za-z]

EDIT: Okay, it seems there's more to it. 编辑:好的,它似乎还有更多。 What I think might suit is: 我认为可能适合的是:

^(?=(earth\b)).*((?:^|\s)\1)$

For the work earth. 为了工作地球。 For any word stored in a variable named word ; 对于存储在名为word的变量中的任何word ;

>>> word = 'earth' # Makes it so you can change it anytime
>>> pattern = re.compile('^(?=(' + word + '\b)).*((?:^|\s)\1)$')
>>> m.search(pattern, s)

Accepts: 接受:

earth is earth
earth

Rejects: 拒绝:

earthearth
eartheearth
earthis earth

And after that extract the captured groups or check whether the group are empty or not. 然后提取捕获的组或检查组是否为空。

The bit I added is (?:^|\\s) which checks for whether the word you're looking for is the only one in the 'sentence' or whether the word is in a sentence. 我添加的位是(?:^|\\s) ,它检查您要查找的单词是否是“句子”中唯一的单词,或者单词是否在句子中。

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

相关问题 如何在一个以相同字母开头和结尾的句子中找到一个单词? - How to find a word in a sentence that starts and ends with the same letter? function 查找以相同字母开头和结尾的单词 - function to find words that starts and ends with the same letter 如何查找以{{并以}结尾的字符串中的所有子字符串 - How to find all substrings in a string that starts with {{ and ends with } 如果Python中字符串以“*”结尾和开头 - If string ends and starts with "*" in Python 在以 python 中的特定字符开头和结尾的字符串中查找并打印 substring 的索引 - Find and print the indexes of a substring in string that starts and ends with a specific character in python Pandas 正则表达式:将名称与以单词或字符串开头并以某些单词结尾的字符串分开 - Pandas Regex: Separate name from string that starts with word or start of string, and ends in certain words Python 正则表达式如何找到以给定单词开头并以两个单词之一结尾的 substring - Python regex how to find a substring that starts with a given word and ends with either of two words 检查字符串是否以 XXXX 开头 - Checking whether a string starts with XXXX 如何在Python中的文件中查找模式的开头和结尾 - How to find pattern starts and ends with in a file in python 查找所有以特定字符开头和结尾的子字符串 - find all substring that starts and ends with specific characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM