在Python中使用正则表达式将句子拆分到特定的“空格”字符下面

2024-09-27 07:29:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在试图解决一个问题,将一个句子拆分成一组特定长度的有意义的单词

string1 = "Alice is in wonderland"
string2 = "Bob is playing games on his computer"

我希望有一个正则表达式,它匹配符合小于20个字符条件的代表性单词

new_string1 = "Alice is in"
new_string2 = "Bob is playing games"

这可以用正则表达式实现吗


Tags: innewison单词games句子意义
1条回答
网友
1楼 · 发布于 2024-09-27 07:29:04

这不是正则表达式的一个好用例。尽管如此,^{}方法确实实现了这一点

import textwrap

string1 = "Alice is in wonderland"
string2 = "Bob is playing games on his computer"

new_string1 = textwrap.shorten(string1, 20, placeholder="")
new_string2 = textwrap.shorten(string2, 20, placeholder="")

print(new_string1) # Alice is in
print(new_string2) # Bob is playing games

{}唯一的缺点是它会压缩空间。如果您不希望发生这种情况,您可以实现自己的方法

def shorten(s, max_chars):
    # Special case is the string is shorter than the number of required chars
    if len(s) <= max_chars:
        return s.rstrip()

    stop = 0
    for i in range(max_chars + 1):
        # Always keep the location of the last space behind the pointer
        if s[i].isspace():
            stop = i

    # Get rid of possible extra space added on the tail of the string
    return s[:stop].rstrip()

string1 = "Alice is in wonderland"
string2 = "Bob is playing games on his computer"

new_string1 = shorten(string1, 20)
new_string2 = shorten(string2, 20)

print(new_string1) # Alice is in
print(new_string2) # Bob is playing games

相关问题 更多 >

    热门问题