Regex for getting all digits in a string after a character

前端 未结 2 1578
花落未央
花落未央 2021-01-20 00:38

I am trying to parse the following string and return all digits after the last square bracket:

C9: Title of object (foo, bar) [ch1, CH12,c03,4]
2条回答
  •  时光说笑
    2021-01-20 01:18

    It may help to use the non-greedy ?. For example:

    \[.*?(\d*?),.*?(\d*?),.*?(\d*?),.*?(\d*?)\]
    

    And, here's how it works (from https://regex101.com/r/jP7hM3/1):

    "\[.*?(\d*?),.*?(\d*?),.*?(\d*?),.*?(\d*?)\]"
    \[ matches the character [ literally
    .*? matches any character (except newline)
    Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
    1st Capturing group (\d*?)
    \d*? match a digit [0-9]
    Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
    , matches the character , literally
    .*? matches any character (except newline)
    Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
    2nd Capturing group (\d*?)
    \d*? match a digit [0-9]
    Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
    , matches the character , literally
    .*? matches any character (except newline)
    Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
    3rd Capturing group (\d*?)
    \d*? match a digit [0-9]
    Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
    , matches the character , literally
    .*? matches any character (except newline)
    Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
    4th Capturing group (\d*?)
    \d*? match a digit [0-9]
    Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
    \] matches the character ] literally
    

    Although - I have to agree with others... This is a regex solution, but its not a very pythonic solution.

提交回复
热议问题