Using 1.*1, * is greedy - it will match all the way to the end, and then backtrack until it can match 1, leaving you with 1010000000001. .*? is non-greedy. * will match nothing, but then will try to match extra characters until it matches 1, eventually matching 101. All quantifiers have a non-greedy mode: .*?, .+?, .{2,6}?, and even .??. http://www.rexegg.com/regex-quantifiers.html#cheat_sheet https://stackoverflow.com/questions/3075130/what-is-the-difference-between-and-regular-expressions