Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A single regular expression match must be contiguous; that is, it cannot exclude some section of text in the middle of the match. However, you could simply perform a replacement, like this:</p> <pre><code>var input = "SAINT GEORGE 378 DEPARTMENT 808 HOUSE 3 C AND REST OF ADDRESS"; var pattern = @"(?i)house\s+\d+[-\s]?[a-zA-Z]?\b\s*"; var output = Regex.Replace(input, pattern, ""); Console.WriteLine(output); // "SAINT GEORGE 378 DEPARTMENT 808 AND REST OF ADDRESS" </code></pre> <p>I added <code>\s*</code> to the end of your pattern to swallow up any trailing spaces. Without this, when I joined the result back together, there would have been two spaces between <code>"808"</code> and <code>"AND"</code>.</p> <hr> <p>If you don't want to do that, most regular expression engines allow you to split a string based on whatever matches you find. The result is usually an array (or array-like structure) containing the parts of the string surrounding the match but not including the match itself (unless that option is enabled, depending on the engine you're using).</p> <p>The easiest solution would be to split the string, then join the results back together. </p> <p>For example, this will work in JavaScript:</p> <pre><code>var input = "SAINT GEORGE 378 DEPARTMENT 808 HOUSE 3 C AND REST OF ADDRESS"; var output = input.split(/house\s+\d+[-\s]?[a-zA-Z]?\b\s*/i).join(""); console.log(output); // "SAINT GEORGE 378 DEPARTMENT 808 AND REST OF ADDRESS" </code></pre> <p>And here is a working example in C#:</p> <pre><code>var input = "SAINT GEORGE 378 DEPARTMENT 808 HOUSE 3 C AND REST OF ADDRESS"; var pattern = @"(?i)house\s+\d+[-\s]?[a-zA-Z]?\b\s*"; var output = string.Join("", Regex.Split(input, pattern)); Console.WriteLine(output); // "SAINT GEORGE 378 DEPARTMENT 808 AND REST OF ADDRESS" </code></pre> <p>Notice in both cases, I got rid of the group because it isn't necessary, but I could have also used a non-capturing group, <code>(?…)</code>. In either case, this is needed because you in both JavaScript and in .NET, if you have a capturing group within the pattern, the result set will contain the matched group along with the parts that surround the entire pattern. </p> <hr> <p>If you don't like that, you could capture the whole string with leading and trailing groups, then concatenate them together:</p> <pre><code>var input = "SAINT GEORGE 378 DEPARTMENT 808 HOUSE 3 C AND REST OF ADDRESS"; var pattern = @"(.*)(?i)house\s+\d+[-\s]?[a-zA-Z]?\b\s*(.*)"; var match = Regex.Match(input, pattern); var output = match.Groups[1].Value + match.Groups[2].Value; Console.WriteLine(output); // "SAINT GEORGE 378 DEPARTMENT 808 AND REST OF ADDRESS" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload