Here I have a string.
word = '10-7-2022 product_id:10 order_id:2 cost:500'
Here, I need to extract only the product_id's and order_id's value which is after ':'. Kindly let me know how to make it happen.
Here I have a string.
word = '10-7-2022 product_id:10 order_id:2 cost:500'
Here, I need to extract only the product_id's and order_id's value which is after ':'. Kindly let me know how to make it happen.
One other method is to use regex with named groups:
import re
reg = re.compile(r"(?P<product_id>(?<=product_id:)\d+).*(?P<order_id>(?<=order_id:)\d+)")
word = '10-7-2022 product_id:10 order_id:2 cost:500'
result = reg.search(word)
result.group('product_id')
result.group('order_id')
If not every string contain product_id:
or order_id:
then you can create a more general regex, and test if there is a result.
EDIT
Also getting the date, assuming that it would always be in the order of d-m-y with or without leading zeros:
import re
reg = re.compile(r"(?P<date>\d+-\d+-\d+).*(?P<product_id>(?<=product_id:)\d+).*(?P<order_id>(?<=order_id:)\d+)")
word = '10-7-2022 product_id:10 order_id:2 cost:500'
result = reg.search(word)
result.group('date')
You can also get a datetime object:
from datetime import datetime
datetime.strptime(result.group('date'), r'%d-%m-%Y')
>>> datetime.datetime(2022, 7, 10, 0, 0)