简体   繁体   English

在 python 中使用正则表达式从多行变量中查找匹配的字符串

[英]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.目前,我的 output 是空的。 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:当前Output:

test  []

Wanted Output:通缉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:Liquidity:
  • Starts with Token Price:Token Price:
  • Starts with 0x0x
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://regex101.com/r/1fqORj/1

https://trinket.io/python/99b59d62b8 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