I have such string:
something: 20 kg/ something: 120 kg
I have this regex ("[0-9]{1,2} kg", string), but it returns 20kg both times. I need to return 20kg only in first case.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try this:
The
(?<!...)is a negative look behind. So it matches one or two digits not preceded by a digit. I also changed the literal space with one or more space chars.Seeing you’ve asked Python questions, here’s a demo in Python:
which will print
['20 kg']edit
As @Tim mentioned, a word boundary
\bis enough:r'\b\d{1,2}\s+kg'