簡體   English   中英

如何通過沒有空格的 substring 分割字符串,同時保留其原始空格?

[英]How to split a string by a substring without white spaces, while keeping its original white spaces?

我正在尋找一種方法,通過刪除了空格的目標短語來拆分帶有空格(包括空格、\n、\t)的字符串。 這應該能夠在目標短語之前和之后完成。 我還必須保留原始字符串及其空格。

由於目標短語可能第 n 次出現,我只希望按第一次出現拆分並獲取它之前的字符,並按最后一次出現拆分並獲取它之后的字符。

例如:

str = 'This is a test string for my test string example only.'
target_phrase = 'teststring'

預期 output:

('This is a', 'test string for my test string example only.) #Split by target phrase and getting characters prior to it
('This is a test string for my test string', 'example only.') #Split by target phrase and getting characters after it

強調文本

感激地收到任何提示。

這是可以接受的嗎(當找不到目標短語時,它不會費心處理這種情況):

# Splits str at the first occurrence of targ, ignoring spaces in both.
# Returns tuple of substrings produced by the split.
def my_split(str, targ):
    idx = str.replace(' ', '').index(targ)

    # Next, in the original string that has spaces,
    # we count the number of spaces and non-spaces, until
    # the number of non-spaces reaches idx. When that happens,
    # it means we have reached the split-point in the original
    # string that has spaces.
    non_space = 0
    space = 0
    while (non_space < idx) and ((non_space+space) < len(str)):
        if str[space+non_space] == ' ':
            space += 1
        else:
            non_space += 1
    if (space + non_space):
        return (str[:space+non_space], str[1+space+non_space:])
    else:
        return ('', str)

用法:

print (my_split(str, target_phrase))
print (tuple(s[::-1] for s in my_split(str[::-1], target_phrase[::-1]))[::-1])

Output:

('This is a', 'test string for my test string example only.')
('This is a test string for my test string', 'example only.')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM