title: How do I regex match each individual word within backticks?
body: <p>I am trying to get results for each individual word within backticks. For example,
  if I have something like this text</p>
  <p>some description `match these_words th_is_wor` or `THIS_WOR thi_sqw` a `word_snake`</p>
  <p>I want the search results to be:</p>
  <ul>
  <li>match</li>
  <li>these_words</li>
  <li>th_is_wor</li>
  <li>THIS_WOR</li>
  <li>thi_sqw</li>
  <li>word_snake</li>
  </ul>
  <p>I'm essentially trying to get each &quot;word&quot;, word being one or more english letter or underscore characters, between each set of backticks.</p>
  <p>I currently have the following regex that seems to match ALL the text between each set of backticks:</p>
  <p>/(?&lt;=`)(\b([^`\]|\w|_)*\b)(?=`)/gi</p>
  <p>This uses a positive lookbehind to find text that comes after a ` character: (?&lt;=`)</p>
  <p>Followed by a capture group for one or more things such that the thing is not a `, not a \, is a word character, or is an _ character within word boundaries: (\b([^`\]|\w|_)*\b)</p>
  <p>Followed by a positive lookahead for another ` character to ensure we're enclosed within backticks.</p>
  <p>This sort of works, but captures ALL the text between backticks instead of each individual word. This would require further processing which I'd like to avoid. My regex results right now are:</p>
  <ul>
  <li>match these_words th_is_wor</li>
  <li>THIS_WOR thi_sqw</li>
  <li>word_snake</li>
  </ul>
  <p>If there is a generic formula for getting each individual word within backticks or within quotes, that would be fantastic. Thank you!</p>
  <p>Note: Much appreciated if the answer could be formatted in C#, but not required, as I can do that bit myself if needed.</p>
  <p>Edit: Thank you Mr. إين from Ben Awad's Discord server for the quickest response! This is the solution as proposed by him. Also thank you to everyone who responded to my post, you guys are all AWESOME!</p>
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Text.RegularExpressions;
  class Program {
    static void Main(string[] args) {
      string backtickSentence = &quot;i want to `match these_words th_is_wor` or `THIS_WOR thi_sqw` a `word_snake`&quot;;
      string backtickPattern = @&quot;(?&lt;=^[^`]*(?:`[^`]*`[^`]*)*`(?:[^`]* )*)\w+&quot;;
      string quoteSentence = &quot;some other \&quot;words in a \&quot; sentence be \&quot;gettin me tripped_up AllUp inHere\&quot;&quot;;
      string quotePattern = &quot;(?&lt;=^[^\&quot;]*(?:\&quot;[^\&quot;]*\&quot;[^\&quot;]*)*\&quot;(?:[^\&quot;]* )*)\\w+&quot;;
      // Call Matches method without specifying any options.
      try {
        foreach (Match match in Regex.Matches(backtickSentence, backtickPattern, RegexOptions.None, TimeSpan.FromSeconds(1)))
          Console.WriteLine(&quot;Found '{0}' at position {1}&quot;, match.Value, match.Index);
  
        Console.WriteLine();
        foreach (Match match in Regex.Matches(quoteSentence, quotePattern, RegexOptions.None, TimeSpan.FromSeconds(1)))
          Console.WriteLine(&quot;Found '{0}' at position {1}&quot;, match.Value, match.Index);
      }
      catch (RegexMatchTimeoutException) {} // Do Nothing: Assume that timeout represents no match.
  
      Console.WriteLine();
      // Call Matches method for case-insensitive matching.
      try {
        foreach (Match match in Regex.Matches(backtickSentence, backtickPattern, RegexOptions.IgnoreCase))
          Console.WriteLine(&quot;Found '{0}' at position {1}&quot;, match.Value, match.Index);
  
        Console.WriteLine();
        foreach (Match match in Regex.Matches(quoteSentence, quotePattern, RegexOptions.IgnoreCase))
          Console.WriteLine(&quot;Found '{0}' at position {1}&quot;, match.Value, match.Index);
      }
      catch (RegexMatchTimeoutException) {}
    }
  }
  </code></pre>
  <p>His explanation for this was as follows, but you can paste his regex into regexr.com for more info</p>
  <pre class="lang-cs prettyprint-override"><code>var NOT_BACKTICK = @&quot;[^`]*&quot;;
  var WORD = @&quot;(\w+)&quot;;
  
  var START = $@&quot;^{NOT_BACKTICK}&quot;; // match anything before the first backtick
  var INSIDE_BACKTICKS = $@&quot;`{NOT_BACKTICK}`&quot;; // match a pair of backticks
  var ODD_NUM_BACKTICKS_BEFORE = $@&quot;{START}({INSIDE_BACKTICKS}{NOT_BACKTICK})*`&quot;; // match anything before the first backtick, then any amount of paired backticks with anything afterwards, then a single opening backtick
  
  var CONDITION = $@&quot;(?&lt;={ODD_NUM_BACKTICKS_BEFORE})&quot;;
  var CONDITION_TRUE = $@&quot;(?: *{WORD})&quot;; // match any spaces then a word
  var CONDITION_FALSE = $@&quot;(?:(?&lt;={ODD_NUM_BACKTICKS_BEFORE}{NOT_BACKTICK} ){WORD})&quot;; // match up to an opening backtick, then anything up to a space before the current word
  
  
  // uses conditional matching
  // see https://learn.microsoft.com/en-us/dotnet/standard/base-types/alternation-constructs-in-regular-expressions#Conditional_Expr
  var pattern = $@&quot;(?{CONDITION}{CONDITION_TRUE}|{CONDITION_FALSE})&quot;;
  
  // refined backtick pattern
  string backtickPattern = @&quot;(?&lt;=^[^`]*(?:`[^`]*`[^`]*)*`(?:[^`]* )*)\w+&quot;;
  </code></pre>