The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Extract numbers from text logs

To extract numbers from text logs, you can use regular expressions (regex), which are ideal for identifying numeric patterns in strings. Here’s a general guide and example in Python:

Example: Extract All Numbers (Integers and Decimals)

python
import re log_text = """ 2023-05-17 10:45:12,456 INFO User ID 1345 accessed endpoint /api/data with 3.14 MB response in 200 ms. 2023-05-17 10:45:13,789 ERROR Code 500: Processing failed after 2.56 seconds. """ # Extract integers and decimal numbers numbers = re.findall(r'bd+.d+|bd+', log_text) # Convert to float or int as needed parsed_numbers = [float(num) if '.' in num else int(num) for num in numbers] print(parsed_numbers)

Output:

python
[2023, 5, 17, 10, 45, 12, 456, 1345, 3.14, 200, 2023, 5, 17, 10, 45, 13, 789, 500, 2.56]

Customization Options:

  • Only integers: Use r'bd+b'

  • Only floats/decimals: Use r'bd+.d+b'

  • Exclude timestamps: Add filters to skip date/time patterns

Let me know if you want this done in another language (JavaScript, Bash, etc.), or for specific formats like IPs, timestamps, or monetary values.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About