-1

I have this input

xxxx,123456,sometext123456,123456,anothertext,123,a

and it is repeated again in first of each line (that is sample of a big log file).

Now I want to extract just first number (123456 after the xxxx) of each line. I tried this regex

(?<=,)[^,]+(?=,)

but it's not working for my needs.

alireza71
  • 339
  • 1
  • 3
  • 14

1 Answers1

3

Try this regex: ^.*?(\d+).*$, in which group 1 matches the first number after start of the line. Don't forget multiline flag so ^ and $ also matches start and end of a line.

matches = re.finditer(r"^.*?(\d+).*$", input, re.MULTILINE)
for i, match in enumerate(matches):
    print("n = " + match.group(1))  # Group 1 matches the number
Nicolas
  • 6,611
  • 3
  • 29
  • 73