Is there a way to extract only strings from a specific location?

Asked 2 years ago, Updated 2 years ago, 19 views

24332 34 124\n 25543 341 456\n 45654 9 123 \n

From text like above

124\n 456\n 123\n

Is there a way to extract only? I want to extract only the rightmost text from the text created by being separated by a fixed length.

python

2022-09-22 18:19

1 Answers

Modified to meet the requirements. First, separate it with \n Navigate through an array of separated strings and find the desired word. " The + " character is different from the one without If there is, look for spaces from the index before "+" and if there isn' Find spaces from the last -1 index. (This is because the sample may also have a space before \n.)

message="2433234124\n25543341456\n456549 123 \n456549 12 + 3 \n456549 1 + 2 + 3 \n"
strArray = message.split("\n")

for str in strArray:
        plusPos = str.find(" + ")
        if plusPos == -1:
            startPos = str[:-1].rfind(" ")
        else:
            startPos = str[:plusPos - len(str)].rfind(" ")
        print str[startPos + 1:]


2022-09-22 18:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.