Regular expression question

Asked 2 years ago, Updated 2 years ago, 50 views

Number 1:In the value of number 2,

Put the number 1 in x

How do I put the number 2 in y?

Of course, that's random

awfdjaiffewjj3324 324234:3423423 asdfdfas3

Like this.

python regex

2022-09-20 10:26

2 Answers

If you think of the condition of the number you mentioned as a very simple \d+... https://regex101.com/r/cnoopg/1

import re

# I think you can just take a screenshot twice
regex = r"(\d+):(\d+)"
test_str = "awfdjaiffewjj33:2d4 324234:3423423 asdfd:fas3"
matches = re.finditer(regex, test_str, re.MULTILINE)


2022-09-20 10:26

>>> s = "awfdjaiffewjj3324 324234:3423423 asdfdfas3"
>>> for word in s.split():
    if ":" in word:
        x, y = map(int, word.split(":"))


>>> x
324234
>>> y
3423423


2022-09-20 10:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.