title: How to skip regex Catastrophic Backtracking
body: <p>I guess my input string is too long or/and has new lines.</p>
  
  <p>What should I fix in the regex?</p>
  
  <pre><code>/^.+ (\S+) (\S+) (\S+) #{target} (\S+) (\S+) (\S+) .+$/
  </code></pre>
  
  <p>Edit:</p>
  
  <p>In an input of example (bigger):</p>
  
  <blockquote>
    <p>n software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the <strong>start</strong> of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. When a program</p>
  </blockquote>
  
  <p>I would like assuming target is <strong>start</strong>  to extract the 3 words before and 3 words after</p>
  
  <p>So extracted words: "determined at the" (left) and "of the program" (right)</p>
  
  <p>Edit2:</p>
  
  <p>After @Denys Séguret comment Catastrophic Backtracking is avoided. Regex is built this way in ruby:</p>
  
  <pre><code>regex =""
  regex += "(\\S+) " * numLeft
  regex += " #{target}"
  regex += " (\\S+)" * numRight
  
  pattern = Regexp.new(regex, Regexp::IGNORECASE)
  matches = pattern.match(input)
  </code></pre>