2

When I use Java with this code:

public static void main(String[] args) {
    Pattern p = Pattern.compile("(abc)+");
    String s = "abcabc d abc";
    Matcher m = p.matcher(s);
    while(m.find()){
        System.out.println(s.substring(m.start(), m.end()));
    }
}

It gives me 'abcabc , abc' as I expect.

When I try to do the same using python 3

import re
s = 'abcabc d abc'
p = re.compile('(abc)+')
res = p.findall(s)
print (res)

It gives me strange 'abc', 'abc' What am I doing wrong?

fedorqui 'SO stop harming'
  • 228,878
  • 81
  • 465
  • 523
  • See the explanation in [my answer](http://stackoverflow.com/a/31915134/3832970). Use `re.search` (for a single match) / `re.finditer` (for multiple matches) in this case to get the match in `.group()`, and the last `abc` inside `.group(1)`. – Wiktor Stribiżew Nov 29 '16 at 14:55

0 Answers0