-2

I have a broad set of text like so:

  1. "Hello there. Are you interested in a holiday in New York, then contact [email protected]. Let us help you."
  2. "Hey mr! Are you interested in a holiday in LA, then contact [email protected]. We got you!"
  3. "Welcome to our humbo home. Are you interested in a holiday in France, then contact [email protected]. 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?

1
  • 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). Commented Mar 30, 2020 at 14:38

1 Answer 1

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 = @"";
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.