0

I want to extract specific substrings from a formatted string. For example, the raw string is 19592@click(className='android.widget.TextView',instance='15'):android.widget.TextView@"All".

I'd like to extract "click", "android.widget.TextView", "15", "android.widget.TextView@"All"" from the string above. Is this a problem that can be solved by python regex? I'm not sure which APIs I should use.

Sheffield
  • 355
  • 4
  • 18

2 Answers2

2

Does this answer your question? - https://www.w3resource.com/python-exercises/re/python-re-exercise-47.php

Or else you can use - https://docs.python.org/3/library/configparser.html

Here config parser will parse your string can give you all the values separated by delimiters(@, =, :)

0

I know regex is what you asked for but maybe you could do with plain find which is more efficient:

def find_between(s, first, last):
    first_pos = s.find(first)
    last_pos = s.find(last)
    if first_pos < 0 or last_pos < 0:
        return None
    first_pos = first_pos + len(first)
    return s[first_pos:last_pos]

and then:

s = '19592@click(className=\'android.widget.TextView\',instance=\'15\'):android.widget.TextView@"All"'
print find_between(s, "@", "(")
Out[0]: 'click'

print find_between(s, "className=", ",")
Out[18]: "'android.widget.TextView'"
zenpoy
  • 19,490
  • 9
  • 60
  • 87