Search code examples
pythonregexregex-group

Regex missing number in group


I'm using regex to split a string with a specific format but I'm still missing one number.

The string format is like this: "22TKL;33TKL;22FBL;35TKL". I'd like to receive as output pairs containing number and substrings like: (22, TKL), (33, TKL), (22, FBL), (35, TKL).

Using regex101 to create the expression, I came up with this: "[^;]+([0-9]+)([a-zA-z]+)". And according to regex101, the grouping is like this:

enter image description here

So, I'm unable to match the first number and don't know how to solve this.


Solution

  • Just ([0-9]+)([a-zA-z]+) works fine https://regex101.com/r/sMxFQX/2

    It didnt work for you since [^;]+ consumed the number, cause the number is a charcter thats not ;

    Alternatively, if you do need this check, you can use this [^;]*?([0-9]+)([a-zA-z]+)

    The ? I added makes it a lazy quantifier, meaning it consumes as few characters as it can, thus not matching the first digit if it doesn't have to