Scripts - is it possible to write a 2 phase script?

I need 2 queries to find the data I want:

  1. full text search on a field
  2. if there are entries found - extract token from another field (probably using regexp)
  3. perform additional query - filter the data by the token extracted in step 2.

so the output of first query should be the input to a second filter.
Is it possible to write that kind of script?