Your example is not quite in line with your requirement. If regexp would match the first occurrence of "hello" then with the given capturing subexpression you would get "hi ". If that is what you want, you can use:
regexp {^(.*)hello} $string d s
However, then it would be simpler to:
- search for the first occurrence of "hello": set position [string first "hello" $string]
- then take the substring: set s [string range $string 0 $position-1]
The reason for your regular expression to fail is that regexp looks for the longest substring that matches. You might get by with the non-greedy variant,
regexp {^(.*?)hello} $string d s
but I am not sure it will always work (well, in this case it would, if I interpret the manual page correctly).
Regards,
Arjen