in reply to
export variable outside foreach
What do you mean by "it doesn't work" ? Does it print something wrong, or nothing at all? See How do I post a question effectively?
Since you have replaced your values and have not given any sample data, I can't tell if this is on purpose but in /\Q$string1/ $string1 is interpreted as a literal string, not a regular expression. This means for example that "a*" will match "a*b+c" and not "aaaaaaa". To have your string interpreted as a regex, do not use \Q