1

I have a complicated text file, here is part of it:

& \multirow{2}{*}{52.7} & \multirow{2}{*}{3} & \multirow{2}{*}{$<$ 0.001}\\

I'm interested in the numbers after the {*}. Here is what I tried with no luck:

import re
m = re.findall(r'{\*}{(.+)}', '& \multirow{2}{*}{52.7} & \multirow{2}{*}{3} & \multirow{2}{*} $<$ 0.001}\\')

However, I get the following result:

['52.7} & \\multirow{2}{*}{3} & \\multirow{2}{*}{$<$ 0.001']

I tried many other combinations but I either get the first number (e.g 52.7), or the middle number (3) or the above. How can I get 52.7, 3, $<$ 0.001 in a group (list).

2
  • 2
    Your first regex obviously won't work (quantifiers are greedy), however I am curious as to what you expect your regex to match in your third example, ie \multirow{2}{*}{$<$ 0.001}? Commented Dec 15, 2012 at 23:32
  • I wanted the bit between the last set of curly brackets; $<$ 0.001. It's working now after adding the ?. Commented Dec 15, 2012 at 23:42

3 Answers 3

3

That's because by default + and * operators are greedy. Use non-greedy modification instead:

{\*}{(.+?)}

Reference: http://www.regular-expressions.info/repeat.html ("Watch Out for The Greediness!" section)

Sign up to request clarification or add additional context in comments.

Comments

1

use the following regex expression:

\{\*\}\{(.*?)\}

you should escape all special characters with backslash \ and use non-greedy wildcard .*? in a subclass for result set.

Comments

1
m = re.findall(r'({\*}{([\d|\.?]+?)})+', '& \multirow{2}{*}{52.7} & \multirow{2}{*}{3} & \multirow{2}{*} $<$ 0.001}\\')
[('{*}{52.7}', '52.7'), ('{*}{3}', '3')]

m = re.findall(r'{\*}{([\d|\.?]+?)}+', '& \multirow{2}{*}{52.7} & \multirow{2}{*}{3} & \multirow{2}{*} $<$ 0.001}\\')
['52.7', '3']

m = re.findall(r'{\*}{(.*?)}', '& \multirow{2}{*}{52.7} & \multirow{2}{*}{3} & \multirow{2}{*} $<$ 0.001}\\')
['52.7', '3', '$<$ 0.001']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.