簡體   English   中英

在 python 中使用正則表達式從多行變量中查找匹配的字符串

[英]Using regex in python to find matching strings from a multiline variable

我正在嘗試從變量中獲取一些數據。 它在多行中。 目前,我的 output 是空的。 提前致謝。

    test = re.findall("0x\sN/A",newMessage,re.MULTILINE)
    print ("test ", test)

樣本數據:

🆕 New token

Version: V2

Pair: WBNB-WCH
Liquidity: 22.0000 BNB ($8284.1997)   #-- data to grab
ℹ️ Transaction

Name: WCH
Total Supply: 1,000,000,000 WCH
Token Price: 0.0000 BNB ($0.0000)     #-- data to grab

Holders: 1
Transfers: 1

⛓ BscScan

🥞 Swap on PancakeSwap

➡️ poocoin.app

0xe61cDdDdF26Ac94C214C981FA012AcA805ea4b4D     #-- data to grab
-----------------------------------
Our Chat - YourCryptoHelperChat
Our Main Info Channel - YourCryptoHelper

當前Output:

test  []

通緝Output:

0xe61cDdDdF26Ac94C214C981FA012AcA805ea4b4D
Liquidity: 22.0000 BNB ($8284.1997)
Token Price: 0.0000 BNB ($0.0000)

我以為你的情況很簡單。 所以這是一個簡單的解決方案:

  • Liquidity:
  • Token Price:
  • 0x
import re
find = r"^(Liquidity: .+|Token Price: .+|0x.+)"
test = re.findall(find, value, re.MULTILINE)
for ret in test:
  print (ret)
Result:
Liquidity: 22.0000 BNB ($8284.1997)
Token Price: 0.0000 BNB ($0.0000)
0xe61cDdDdF26Ac94C214C981FA012AcA805ea4b4D

https://regex101.com/r/1fqORj/1

https://trinket.io/python/99b59d62b8

regex = r"Liquidity: ([^#]*).*Token Price: ([^#]*).*(0x[\da-fA-F]{40})"
matches = re.finditer(regex, test_str, re.MULTILINE | re.DOTALL)

暫無
暫無

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

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