简体   繁体   中英

How to get first integer between two specific characters with regex in Python?

I'm parsing some log files and need to extract a integer for a "size" parameter.

The string (part of it) looks like this

 "asdasdasd\\\size\\x22:22\x0A23232d:123123123\x0A2"  

I want to get the first integer between ":" and "\\". That would be 22. Not 123123123.

I have tried the following code

 p = re.compile("[\:](\d+)[\D]")
 s = "asdasdasd\\size\\x22:22\x0A23232d:123123123\x0A2"
 p.findall(s)[0]

 output = '22'

However, if there is no number between the first appearances of ":" and "\\" and want the code to return None or 0. Right now the code will return '123123123' if the pattern looks like this:

"asdasdasd\\size\\x22:\x0A23232d:123123123\x0A2"

What would be the best way to achieve this?

You may use re.search with the following pattern:

p = re.compile(r"^[^:]*:(\d+)")

See the regex demo with String 1 and another demo with String 2 .

Details

  • ^ - start of string
  • [^:]* - 0+ chars other than :
  • : - a -
  • (\\d+) - Capturing group 1: one or more digits

See the Python demo :

import re
strs = ["asdasdasd\\size\\x22:\x0A23232d:123123123\x0A2", "asdasdasd\\\size\\x22:22\x0A23232d:123123123\x0A2"]
p = re.compile(r"^[^:]*:(\d+)")
for s in strs:
    result = ""
    m = p.search(s)
    if m:
        result = m.group(1)
    else:
        result = None
    print(result)

Output:

None
22

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