Escaping special characters for use as a URL

If a url contain special characters (~ and #) using it as

\href{http://people.brunel.ac.uk/~mastmmg/ssguide/set_work.html#4_32}{people.brunel.ac.uk}


works just fine. But, I would like to have macro list which contains these URLs and process them at a later time as I did in Use \foreach loop to execute macro with parameters of macro provided in a list. So, I'd like to be able to say something like:

\DefineMyFormatLinkParameters{%
[people.brunel.ac.uk]{http://people.brunel.ac.uk/~mastmmg/ssguide/set_work.html#4_32},
{Yahoo}{http://www.yahoo.com}
}


With the offending line commented I get:

This works fine for the 1st and 3rd case, but not the one with the special TeX characters. I tried escaping them but that doesn't quite work as the links are not click able.

So, what escapes me is how to escape the special characters in such a list?

Code

\documentclass{article}
\usepackage{url}
\usepackage{pgffor}
\usepackage{xparse}
\usepackage{xstring}

s%   #1 =* not used yet
O{}% #2 = optional title
m%   #3 = Mandatory title
}{%
\par
\hspace*{1.0cm}\href{#4}{#3\IfValueT{#2}{~#2}}%
}%

}%

\begin{document}
\par\hspace*{1.0cm}\href{http://people.brunel.ac.uk/~mastmmg/ssguide/set_work.html#4_32}{people.brunel.ac.uk}

% Prefer to define a list, and later execute the list:
%% What changes do I need to make to the following URL to get it pass through the macros.
% [people.brunel.ac.uk]{http://people.brunel.ac.uk/~mastmmg/ssguide/set_work.html#4_32},
{Yahoo}{http://www.yahoo.com}
}

\bigskip%
Following should produce same results as above:\medskip\par
\typeout{DEBUG: "\x"}
}

\end{document}


\documentclass{article}
\usepackage{url}
\usepackage{pgffor}
\usepackage{xparse}
\usepackage{xstring}

s%   #1 =* not used yet
O{}% #2 = optional title
m%   #3 = Mandatory title
}{%
\par
\hspace*{1.0cm}\href{#4}{#3\IfValueT{#2}{~#2}}%
}%

}%

\begin{document}
\par\hspace*{1.0cm}\href{http://people.brunel.ac.uk/~mastmmg/ssguide/set_work.html#4_32}{people.brunel.ac.uk}

% Prefer to define a list, and later execute the list:
%% What changes do I need to make to the following URL to get it pass through the macros.
{people.brunel.ac.uk}{http://people.brunel.ac.uk/\string~mastmmg/ssguide/set\string_work.html\string#4\string_32},
{Yahoo}{http://www.yahoo.com}
}

\bigskip%
Following should produce same results as above:\medskip\par