0

I would like to extract the numbers in front of "OZ" and "CT"

For example

str = "CHOCOLATE ALMONDS 23OZ 12CT 589"

str2 = "CHOCOLATE ALMONDS 23 OZ 12 CT 3452"

I want to be able to extract 23 and 12 from str or str2 but ignore other numbers listed. The numbers can consist of any number of digits (can be 1, 12, 123, etc.)

I was thinking of using string.find('OZ') and print the number before that based on the index but with that, I have to add logics that can tell the number of digits.

What is the most efficient way to do that? I'm new to Python, so any suggestions would be appreciated.

EDIT: This is NOT a duplicate of this question, as this one requires much more advanced searching than just to find all numbers in the string (as this string contains more numbers than what is requested to be found).

th3n3wguy
  • 3,649
  • 2
  • 23
  • 30
student001
  • 31
  • 7
  • https://docs.python.org/3/howto/regex.html – Tom Dalton May 13 '20 at 19:40
  • You should use regular expressions, implemented in the `re` module of the standard library - https://docs.python.org/3/library/re.html . If you include some sort of reference code in your questions, you would probably get some code fragments as an answer. – Amitai Irron May 13 '20 at 19:42
  • You can also use replace function to remove character from string. – Rima May 13 '20 at 19:44
  • 1
    I'm going to add an answer in the comment in case someone stumbles across this, since it should not have been closed (sorry for formatting, but that's the problem with using a comment instead of an answer): from re import findall regex = '([0-9]+)(?=(OZ|CT|(\sOZ)|(\sCT)))' str1 = "CHOCOLATE ALMONDS 23OZ 12CT" str2 = "CHOCOLATE ALMONDS 23 OZ 12 CT" # list(filter(lambda x: x < 0, number_list)) t = list(map(lambda x: int(x[0]), findall(regex, str1))) t2 = list(map(lambda x: int(x[0]), findall(regex, str2))) print(t) print(t2) – th3n3wguy May 13 '20 at 20:02
  • Thanks @karel. I will watch it. – th3n3wguy May 14 '20 at 20:07

0 Answers0