Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Help with simple regex (Python)

Last post 03-10-2010, 2:46 AM by paolinoZz. 2 replies.
Sort Posts: Previous Next
  •  03-09-2010, 10:54 AM 60589

    Help with simple regex (Python)

    Hi, given a string like "ProductA 1 Product B 1 2 3 ProductC 10 20 50" I need to obtain a list of products and a list of corresponding numbers related to each product.

    The first list is easy to obtain:

    s = "ProductA 1 Product B 1 2 3 ProductC 10 20 50"

    p = re.compile(u"[a-zA-Z]+")

    p.findall(s)

    ["ProductA", "ProductB", "ProductC"]

    Now the second list should be done this way:

    ["1", "1 2 3", "10 20 50"]

    I tried using this regex but it didn't work: (\s+\d+\s*)+ because findall returns [' 1 ', ' 1 ', ' 3 ', ' 10 ', ' 30'] which is totally unuseful to me. I also tried to change regex to (\s+\d+\s*)+? which enables greedy, but still wrong result.

    Do you have any hint... or a solution? ;)

    Thanks!

    Filed under:
  •  03-09-2010, 1:06 PM 60595 in reply to 60589

    Re: Help with simple regex (Python)

    Take the same pattern you used to create your first list and perform a regex split against the original string to create the second list.

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  03-10-2010, 2:46 AM 60636 in reply to 60595

    Re: Help with simple regex (Python)

    Of course, thanks a lot for you answer, this did the trick:

    p = [item.strip().split() for item in p.split(s) if item]

    that gives exactly what I need:

    [['1'], ['1', '2', '3'], ['10', '20', '50']]

View as RSS news feed in XML