-2

I have a broad set of text like so:

  1. "Hello there. Are you interested in a holiday in New York, then contact nyc@mydomain.com. Let us help you."
  2. "Hey mr! Are you interested in a holiday in LA, then contact la@mydomain.com. We got you!"
  3. "Welcome to our humbo home. Are you interested in a holiday in France, then contact france@mydomain.com. See you soon!"

I want to remove the string " Are you interested in a holiday in <place>, then contact <email>."

Here I don't know how many characters <place> has or what exactly the value of <email> is.

The result would be:

  1. "Hello there. Let us help you."
  2. "Hey mr! We got you!"
  3. "Welcome to our humbo home. See you soon!"

I've been trying out and googling for regex replacements with placeholders, but I only find INSERTING values with regex and string.format examples, but nothing on replacing values. I looked here, but in this sample there are already placeholders in the source text.

I also checked here and tested to replace at least the <place>:

txt = Regex.Replace(txt, " Are you interested in a holiday in \d+\.?\d*%;, then", "")
txt = Regex.Replace(txt, " Are you interested in a holiday in \\d+\\.?\\d*%;, then", "")

But nothing gets replaced.

How can I replace the source string that has dynamic and values?

Adam
  • 6,041
  • 36
  • 120
  • 208
  • 1
    `\d+\.?\d*%` matches percentage values. Why not try `.*?` as mentioned in the question you referred to? Any email is usually 1+ non-whitespace chars, `@` and again some non-whitespace, so `\S+@\S+` (of course, it is a simplification, but there are a lot of examples of more restrictive patterns online). At any rate, the pattern to match any text is `.*?`, try this (with `RegexOptions.Singleline` option). – Wiktor Stribiżew Mar 30 '20 at 14:38

1 Answers1

0

You may try that:

Are you interested in a holiday in [^,]+,\s*then contact [^@]+@[\S]+

and replace it by ""

Regex101

string pattern = @"Are you interested in a holiday in [^,]+,\s*then contact [^@]+@[\S]+";
string substitution = @"";
Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43