python – What regex function could I use to find the answers?
Here is the types of string I have in Python:
$\textbf{(A) }4\qquad\textbf{(B) }6\qquad\textbf{(C) }7\qquad\textbf{(D) }10\qquad\textbf{(E) }11$
$\textbf{(A)}\ 2S + 3\qquad \textbf{(B)}\ 3S + 2\qquad \textbf{(C)}\ 3S + 6 \qquad\textbf{(D)}\ 2S + 6 \qquad \textbf{(E)}\ 2S + 12$
$\textbf{(A) }50 \qquad \textbf{(B) }52 \qquad \textbf{(C) }54 \qquad \textbf{(D) }56 \qquad \textbf{(E) }58$
$\text{(A) }4^4 5^5 6^6 \qquad \text{(B) }4^4 5^6 6^5 \qquad \text{(C) }4^5 5^4 6^6 \qquad \text{(D) }4^6 5^4 6^5 \qquad \text{(E) }4^6 5^5 6^4$
$\mathrm{(A) \ } \frac{1}{4}\qquad \mathrm{(B) \ } \frac13\qquad \mathrm{(C) \ } \frac38\qquad \mathrm{(D) \ } \frac25\qquad \mathrm{(E) \ } \frac12$
I want to extract the answers from these lines, i.e. the text after the (A), (B), (C), (D) and (E). For example, for the first line, I would like to get:
[4, 6, 7, 10, 11]
For the last line:
[\frac{1}{4}, \frac13, \frac38, \frac25, \frac12]
There is a lot of variation in the lines and I can’t find a regex that can do the job. Any help would be appreciated.
I tried to use something like:
line = "$\textbf{(A) }4\qquad\textbf{(B) }6\qquad\textbf{(C) }7\qquad\textbf{(D) }10\qquad\textbf{(E) }11$"
A = re.findall(r"(?<=A)(.*)(?=B)", line)
and I get:
[') }4\\textbf{(']
to find what was between A and B and do something similar for the other answers but there was always “junk” that I could’nt get rid off.
Read more here: Source link
