如何提取后面没有关键字的数字

我想从字符串中获取十进制数字,但后面不跟“/套”。


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']

如何得到正确答案?


呼啦一阵风
浏览 102回答 2
2回答

慕妹3146593

头脑:{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

红糖糍粑

如果我理解正确的话,使用向前看和向后看这相当简单......(?<=:)(\d+)(?!.*套)&nbsp; &nbsp; : Using positive look behind to make sure the number is preceded by a colon, doesn't include the colon in the "full match":(\d+)(?!.*套)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Not using positive look behind; colon is included in "full match" but not in groups(?<=:)(\d+)(?!.*套)(?<=&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Start of positive look behind&nbsp; &nbsp; :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : String to search for&nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: End of positive look behind&nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : Start of capture group&nbsp; &nbsp; &nbsp; &nbsp;\d+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Capture any number 1 or more times&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : End of capture group&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(?!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Start of negative look ahead&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .*&nbsp; &nbsp; &nbsp; &nbsp;: Greedy "capture anything" before string we're trying to avoid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 套&nbsp; &nbsp; &nbsp;: String we're trying to avoid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; : End of negative look ahead
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python