It sounds like you want a non-greedy match. Regexps are usually greedy by default. This means they will try to match as much as possible. A non-greedy regular expression will match as little as possible.
Put a question mark ("?") after the end of the expression to make it non-greedy.
Examples:
string "abxcdx", regexp ".*x" matches "abxcdx".
string "abxcdx", regexp ".*?x" matches "abx" and "cdx".