简体   繁体   中英

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

I am trying to grab some data from a variable. It is in a multiline. Currently, my output is empty. Thanks in advance.

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

Sample Data:

🆕 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

Current Output:

test  []

Wanted Output:

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

I assumed your case is simple. So this is simple solution to find:

  • Starts with Liquidity:
  • Starts with Token Price:
  • Starts with 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM