第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用正則表達(dá)式從“1.hello”獲取“hello”

使用正則表達(dá)式從“1.hello”獲取“hello”

我剛剛學(xué)習(xí)正則表達(dá)式,但無(wú)法從列表中獲取單詞從這樣的列表中:[ "1. hello - jeff", "2. gello - meff", "3. fellow - gef", "12. willow - left"]我想檢索單詞:“hello”、“gello”、“fellow”和“willow”這是到目前為止我的簡(jiǎn)化代碼for i in [ARRAY OF LISTED WORDS]:   word = re.findall(r'^((?![0-9]?[0-9]. ))\w+', i)     print(word)
查看完整描述

2 回答

?
弒天下

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊

您正在查找數(shù)字之間的一個(gè)或多個(gè)非空格'\S+'),后跟句點(diǎn),后跟空格 ( '\d+\.\s'),以及空格后跟短劃線 ( '\s-'):

pattern = r'\d+\.\s(\S+)\s-'
[re.findall(pattern, l)[0] for l in your_list]


查看完整回答
反對(duì) 回復(fù) 2023-10-11
?
德瑪西亞99

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊

你的正則表達(dá)式模式:


pattern = r"""

    \d+     # 1 or more digits

    \.      # Escaped period character

    \s+?    # 1 or more whitespace

    (\w+)   # 1 or more alphabetic characters

    \s+     # 1 or more whitespace

    -       # hyphen

    .*      # zero or more of anything besides newline.

"""

字符串列表:


words = [ "1. hello - jeff", "2. gello - meff", "3. fellow - gef", "12. willow - left"]



for word in words:

    # capture results in a variable

    # re.X for verbose pattern format.

    tmp = re.search(pattern, word, flags = re.X)

    # If variable is not None, print results of the first captured group.

    if tmp:

        print(tmp.group(1))

輸出:


hello

gello

fellow

willow


查看完整回答
反對(duì) 回復(fù) 2023-10-11
  • 2 回答
  • 0 關(guān)注
  • 144 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)