亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python如何使用特殊符號搜索子字符串?和 *(類似 RexExp)

Python如何使用特殊符號搜索子字符串?和 *(類似 RexExp)

瀟湘沐 2022-06-28 10:28:19
?我應該通過包含子字符串和兩個可識別的字符模式來過濾字符串數組 *。?- 一個任意符號*- 任何數量的任何符號以數據為例,['baab', 'abbb', 'fc', 'AA']它應該如下工作:'?b' -> ['baab', 'abbb']'?a' -> ['baab']'c?' -> []'b??b' -> ['baab']'???' -> ['baab', 'abbb']'b*b' -> ['baab', 'abbb']'***' -> ['baab', 'abbb', 'fc', 'AA']我不能使用in運算符。最簡單的解決方法是什么?也許它應該是 RegExp(但我不確定)或其他東西。
查看完整描述

3 回答

?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

您應該使用正則表達式: 對于任何字符,使用一個點 - 對于任意數量的任何字符,使用一個點后跟一個星號 - 對于 0 或 1 個字符,使用一個點后跟一個問號。


import re

define shortlist (pattern, list):

    return [s for s in list if re.search(pattern, s)]


mylist = ['baab', 'abbb', 'fc', 'AA']

l = shortlist('.b', mylist)  # l = ['baab', 'abbb']

l = shortlist('.a', mylist)  # l = ['baab']

l = shortlist('b..b', mylist)  # l = ['baab']

l = shortlist('b.*b', mylist)  # l = ['baab', 'abbb']

l = shortlist('.{3}', mylist) # l = ['baab', 'abbb']

查看https://docs.python.org/3/library/re.html以獲取完整參考。


查看完整回答
反對 回復 2022-06-28
?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

您可以通過替換“?”來修復模式 with.和*with .*?in recognizable character patterns并返回substring至少包含一個匹配項 ( re.search) 的任何內容:


import re


example = ['baab', 'abbb', 'fc', 'AA']


def filter_by_pattern(elements, pattern):

    # convert `?` and `*` flags

    pattern = pattern.replace('?', '.').replace('*', '.*?')

    # you could make this a generator instead of list if you like

    return [substring for substring in elements if re.search(pattern, substring)]


print(filter_by_pattern(example , '?b'))

print(filter_by_pattern(example , '?a'))

print(filter_by_pattern(example , 'c?'))

print(filter_by_pattern(example , 'b??b'))

print(filter_by_pattern(example , '???' ))

print(filter_by_pattern(example , 'b*b' ))

print(filter_by_pattern(example , '***' ))

輸出:


['baab', 'abbb']

['baab']

[]

['baab']

['baab', 'abbb']

['baab', 'abbb']

['baab', 'abbb', 'fc', 'AA']


查看完整回答
反對 回復 2022-06-28
?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

一種正則表達式方法是 1) 收縮所有連續*的 s,2)和3re.escape之間的部分3) 在使用修飾符編譯正則表達式時用和替換每個:?**.*?.re.DOTALL


import re


def repl(m):

    res = "{}{}".format(re.escape(m.group(1)), m.group(2).replace("*", ".*").replace("?", "."))

    if m.group(3):

        res += re.escape(m.group(3))

    return res


def glob_to_regex(glob):

    glob = re.sub(r'\*{2,}', '*', glob)

    return '(?s)' + re.sub(r'([^?*]*)([*?]+)([^?*]+$)?', repl , glob)


l = ['baab', 'abbb', 'fc', 'AA', 'abb.']

print([x for x in l if re.search(glob_to_regex('?b*b'), x)])

print([x for x in l if re.search(glob_to_regex('?b*.'), x)])

請參閱Python 演示。

在這里,一個類似的模式?b***將被翻譯成一個(?s).b.*模式,這意味著:

  • (?s)- 一個內聯re.DOTALL修飾符,也使.匹配換行符

  • .- 任何 1 個字符

  • b- 一個b字符

  • .*- 盡可能多的任何 0+ 字符。

如果您需要支持任何glob模式,則需要fnmatch.filter(names, pattern)接受glob模式的方法(這就是您的?*在此處的通配符)。

這里的獨特問題是,fnmatch當您的模式未錨定時,glob 必須匹配整個輸入。

因此,您需要用*s 包裝您的模式:

import fnmatch

l = ['baab', 'abbb', 'fc', 'AA']

print(fnmatch.filter(l, '*{}*'.format('?b')))

print(fnmatch.filter(l, '*{}*'.format('?a')))

print(fnmatch.filter(l, '*{}*'.format('c?')))

print(fnmatch.filter(l, '*{}*'.format('b??b')))

print(fnmatch.filter(l, '*{}*'.format('???')))

print(fnmatch.filter(l, '*{}*'.format('b*b')))

print(fnmatch.filter(l, '*{}*'.format('***')))

輸出:


['baab', 'abbb']

['baab']

[]

['baab']

['baab', 'abbb']

['baab', 'abbb']

['baab', 'abbb', 'fc', 'AA']


查看完整回答
反對 回復 2022-06-28
  • 3 回答
  • 0 關注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號