Best way to parse/find text with Python

663 views Asked by At

I have a file that I've read into memory and I would like to parse or search that particular string for all occurrences of "TEST_COMMAND". I could have commands like TEST_COMMAND.1, TEST_COMMAND.2, etc. I simply want to find all of these occurances of TEST_COMMAND and then split on the period (.) so I can get the data on the right of the period. For example, TEST_COMMAND.1 and TEST_COMMAND.2 would result in adding 1, 2 to the list.

I've been messing around with str.find and parse but can't seem to put anything together that will work. If any of you have some ideas I would love to hear them.

Here is some updated data. Need to parse a file with the following:

function TEST_CMD.OFF(uuid, uprm) function TEST_CMD.NUMBER_1(uuid, uprm)

Basically I would need to parse this file take whatever is aafter the . but before the (uuid, urpm). In this case I would like the output to be ['OFF', 'NUMBER_1']

1

There are 1 answers

4
Fredrik Pihl On BEST ANSWER

Try something like this:

In [8]: s
Out[8]: 'some string, TEST_COMMAND.1, some other string, TEST_COMMAND.2'

In [9]: import re

In [10]: re.findall(r'TEST_COMMAND\.(\d+)', s)
Out[10]: ['1', '2']

Update

Try

In [20]: re.findall(r'TEST_CMD\.([^(]+)', ss)
Out[20]: ['OFF', 'NUMBER_1']

for your second question