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

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

如何提取后面沒有關鍵字的數字

如何提取后面沒有關鍵字的數字

呼啦一陣風 2024-01-12 10:18:09
我想從字符串中獲取十進制數字,但后面不跟“/套”。pattern = '(\d{1,}\.{0,1}\d{0,}萬{0,1}-{0,1}\d{0,}\.{0,1}\d{0,}萬{0,1})元{0,1}(?!/套)'string1 = 'item 1:298元/套起'string2 = 'item 1:298元/m2起'string3 = 'item 1:298/m2起'result1 = re.findall(pattern, string1) #expected [], but return ['1', '298']result2 = re.findall(pattern, string2) #expected [298], but return ['1', '298', '2']result3 = re.findall(pattern, string3) #expected [298], but return ['1', '298', '2']如何得到正確答案?
查看完整描述

2 回答

?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

頭腦:

  • {0,1} = ?

  • {0,}=*

  • {1,}=+

使用

(\d+\.?\d*萬?-?\d*\.?\d*萬?)元?/(?!套)

查看證明

解釋

--------------------------------------------------------------------------------

  (                        group and capture to \1:

--------------------------------------------------------------------------------

    \d+                      digits (0-9) (1 or more times (matching

                             the most amount possible))

--------------------------------------------------------------------------------

    \.?                      '.' (optional (matching the most amount

                             possible))

--------------------------------------------------------------------------------

    \d*                      digits (0-9) (0 or more times (matching

                             the most amount possible))

--------------------------------------------------------------------------------

    萬                  '萬'

--------------------------------------------------------------------------------

    ;?                       ';' (optional (matching the most amount

                             possible))

--------------------------------------------------------------------------------

    -?                       '-' (optional (matching the most amount

                             possible))

--------------------------------------------------------------------------------

    \d*                      digits (0-9) (0 or more times (matching

                             the most amount possible))

--------------------------------------------------------------------------------

    \.?                      '.' (optional (matching the most amount

                             possible))

--------------------------------------------------------------------------------

    \d*                      digits (0-9) (0 or more times (matching

                             the most amount possible))

--------------------------------------------------------------------------------

    萬                  '萬'

--------------------------------------------------------------------------------

    ;?                       ';' (optional (matching the most amount

                             possible))

--------------------------------------------------------------------------------

  )                        end of \1

--------------------------------------------------------------------------------

  元                  '元'

--------------------------------------------------------------------------------

  ;?                       ';' (optional (matching the most amount

                           possible))

--------------------------------------------------------------------------------

  /                        '/'

--------------------------------------------------------------------------------

  (?!                      look ahead to see if there is not:

--------------------------------------------------------------------------------

    套                 '套'

--------------------------------------------------------------------------------

  )                        end of look-ahead


查看完整回答
反對 回復 2024-01-12
?
紅糖糍粑

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

如果我理解正確的話,使用向前看和向后看這相當簡單......


(?<=:)(\d+)(?!.*套)    : Using positive look behind to make sure the number is preceded by a colon, doesn't include the colon in the "full match"

:(\d+)(?!.*套)         : Not using positive look behind; colon is included in "full match" but not in groups



(?<=:)(\d+)(?!.*套)

(?<=                   : Start of positive look behind

    :                  : String to search for

     )                 : End of positive look behind

      (                : Start of capture group

       \d+             : Capture any number 1 or more times

          )            : End of capture group

           (?!         : Start of negative look ahead

              .*       : Greedy "capture anything" before string we're trying to avoid

                套     : String we're trying to avoid

                  )    : End of negative look ahead


查看完整回答
反對 回復 2024-01-12
  • 2 回答
  • 0 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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