Match up to a specific word in a multi-line string

I am trying to filter out some garbage text from a regex string, but it can't seem to get it to work. I am not a regular expression expert (not even close), and I was looking for similar examples, but none of them seemed to solve my problem.

I need a regular expression that matches all values ​​from the beginning of a line to a specific word in that line, but not the word itself.

here is an example:

<p>This is the string I want to process with as you can see also contains HTML tags like <i>this</i> and <strong>this</strong></p> <p>I want to remove everything in the string BEFORE the word "giraffe" (but not "giraffe" itself and keep everything after it.</p> 

So, how can I combine everything in the line before the word "giraffe"?

Thanks!

+4
source share
5 answers
 resultString = Regex.Replace(subjectString, @"\A # Start of string (?: # Match... (?!""giraffe"") # (unless we're at the start of the string ""giraffe"") . # any character (including newlines) )* # zero or more times", "", RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace); 

must work.

+5
source

Why regex?

 String s = "blagiraffe"; s = s.SubString(s.IndexOf("giraffe")); 
+4
source

Try the following:

  var s = @"<p>This is the string I want to process with as you can see also contains HTML tags like <i>this</i> and <strong>this</strong></p> <p>I want to remove everything in the string BEFORE the word ""giraffe"" (but not ""giraffe"" itself and keep everything after it.</p>"; var ex = new Regex("giraffe.*$", RegexOptions.Multiline); Console.WriteLine(ex.Match(s).Value); 

This piece of code produces the following output:

 giraffe" (but not "giraffe" itself and keep everything after it.</p> 
+1
source

A look-ahead would do the trick:

 ^.*(?=\s+giraffe) 
0
source

You could use a template with this view

^.*?(?=giraffe)

0
source

Source: https://habr.com/ru/post/1392696/


All Articles