title: What is the most accurate way to sniff out substrings in unpredictable strings of words in Ruby?
body: <p>I'm working on a cash register.  The input is lists of items, like the following:</p>
  
  <pre><code>1 table at 92.49
  1 chair at 44.59
  1 imported rug at 580.99
  1 set of imported cups at 83.79
  </code></pre>
  
  <p>As you can see, the position of the word "imported" does not follow a fixed standard, but I want the output to be as follows (notice the word "at" is also omitted and replaced by a colon):</p>
  
  <pre><code>1 table: 92.49
  1 chair: 44.59
  1 imported rug: 580.99
  1 imported set of cups: 83.79
  </code></pre>
  
  <p>I did this by</p>
  
  <pre><code>.each do |line|
  </code></pre>
  
  <p>for the input file, and removing array.first and array.last of line.split</p>
  
  <pre><code>    getElements = []
      getElements &lt;&lt; line.split
  
      leftSnippet = getElements.first.first
  
      rightSnippet = getElements.last.last
  
      itemName = line.delete! leftSnippet + rightSnippet
  
      sort_Imported = []
  
        if sort_Imported[0] == "imported"
          itemName = itemName.gsub!(/ imported/, "")
          itemName = itemName[0...-5]
  
        elsif sort_Imported.include? "imported"
          itemName = itemName.gsub!(/ imported/, "")
          itemName = itemName[0...-4]
  
        else itemName = itemName[0...-5]
      end
  </code></pre>
  
  <p>leaving me with something like follows:</p>
  
  <pre><code>table at
  chair at
  imported rug at
  set of imported cups at
  </code></pre>
  
  <p>The reason I wrote this part:</p>
  
  <pre><code>sort_Imported = []
  sort_Imported &lt;&lt; newItemName.split
  sort_Imported = sort_Imported.first
  
      if sort_Imported[0] == "imported"
        itemName = itemName.gsub!(/ imported/, "")
        itemName = itemName[0...-5]
  
    elsif sort_Imported.include? "imported"
      itemName = itemName.gsub!(/ imported/, "")
      itemName = itemName[0...-5]
  
    else itemName = itemName[0...-5]
  end
  </code></pre>
  
  <p>...is because some very strange things were happening when I added the ":" and the prices at the end involving spaces and missing characters.  Something like follows:</p>
  
  <pre><code>table :
  chair:
  imported rug:
  imported set of  cup:
  </code></pre>
  
  <p>The messing with the -4 and -5 allowed me to get the result I wanted for my data set, BUT I don't like how it is hard-coded in because if I have different types of items the same kind of problem will arise.</p>
  
  <p>So I'm wondering am I going about this all wrong, if there is a less clumsy way to get these substrings?  It's the only logical conclusion I have been able to reach since the nature of the item names is that they could literally be anything.</p>