0

I have string like

"FirstName=Alpha&LastName=Beta&[email protected]&Country=UnitedStates&[email protected]"

I need some help with the regular expression with extracts the email value.

I used the pattern "Email=(.*?)&" but it needs an & at the end of the string.

My Code to replace the email value

Regex.Replace(text, "email=(.*?)&", ComputeReplacement, RegexOptions.IgnoreCase);

Any help with regex?

2
  • You have two different emails in the example string. What are you trying to replace them with? Commented Sep 13, 2016 at 19:16
  • I want to replace the email with a text like "something" Commented Sep 13, 2016 at 21:29

4 Answers 4

4

Strings in .NET are invariant immutable, and the input parameter to this overload of Regex.Replace is not a ref parameter. Instead, the changed string will be returned by Regex.Replace. Simply assign it:

text = Regex.Replace(text, "email=(.*?)&", ComputeReplacement, RegexOptions.IgnoreCase);

I'm assuming (though we all know what effect that has on u and me) that ComputeReplacement is a MatchEvaluator.

4
  • 1
    I'm going to be an insufferable pedant and point out that strings are immutable. Invariant usually refers to generics (invariant is what you get when something is neither covariant nor contravariant).
    – Kyle
    Commented Sep 13, 2016 at 19:26
  • 1
    @Kyle Thank you. I have some kind of weird aphasia with that word. Your pedantry is entirely sufferable. Commented Sep 13, 2016 at 19:32
  • My question should have been more clear. The given code does not work for the input string because the second occurrence of email does not end with a string. Need help with regex and then I need to replace all the email values with a computed value like email.Length. Commented Sep 13, 2016 at 21:30
  • The regex that worked for me is - @"email=(?<match>[^&]*)&?" and then i used below to get it to work. MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); foreach (Match match in matches) { inputText.Replace(match.Result(string.Format("${{{0}}}", namedGroup)), GetScrubbedPIIString(match.Result(string.Format("${{{0}}}", namedGroup)))); } namedGroup is "match" Commented Sep 27, 2016 at 18:11
3

Maybe you could take a look into the HttpUtility.ParseQueryString method, as it can handle the same task without the regular expression:

var query = "FirstName=Alpha&LastName=Beta&[email protected]&[email protected]";
var parsed = HttpUtility.ParseQueryString(query);
foreach (var email in parsed.GetValues("Email"))
{
    Console.WriteLine(email);
}

This approach also takes care of the URL encoding; for instance, try with "FirstName=Some%20Name"

0
0

You can use the following code to get the emails:

string input = "FirstName=Alpha&LastName=Beta&[email protected]&Country=UnitedStates&[email protected]";
Dictionary<string,string> emails = input.Split(new char[] { '&' }).Where(param => param.ToLower().Contains("email")).Select(param => param.Split(new char[] { '=' })).ToDictionary(x=> x[1], x=> x[0]);
foreach (string email in emails.Keys)
{
    Console.WriteLine(email);
}
0

if you split the string before using your Regex, there is no need anymore for an & at the end of the string (you will end up with an extra & at the end of the resulting string, but you can check for that if it is an issue):

String input = "FirstName=Alpha&LastName=Beta&[email protected]&Country=UnitedStates&[email protected]";
String output = "";
foreach (string element in input.Split('&')) {
    output += Regex.Replace(element, "email=(.*?)&", ComputeReplacement, RegexOptions.IgnoreCase) + "&"; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.