Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7649817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:04:02+00:00 2026-05-31T11:04:02+00:00

I need to search for and mark patterns which are split somewhere on a

  • 0

I need to search for and mark patterns which are split somewhere on a line. Here is a shortened list of sample patterns which are placed in a separate file, e.g.:

CAT,TREE
LION,FOREST
OWL,WATERFALL

A match appears if the item from column 2 ever appears after and on the same line as the item from column 1. E.g.:

THEREISACATINTHETREE. (matches)

No match appears if the item from column 2 appears first on the line, e.g.:

THETREEHASACAT. (does not match)

Furthermore, no match appears if the item from column 1 and 2 touch, e.g.:

THECATTREEHASMANYBIRDS. (does not match)

Once any match is found, I need to mark it with \start{n} (appearing after the column 1 item) and \end{n} (appearing before the column 2 item), where n is a simple counter which increases anytime any match is found. E.g.:

THEREISACAT\start{1}INTHE\end{1}TREE.

Here is a more complex example:

THECATANDLIONLEFTTHEFORESTANDMETANDOWLINTREENEARTHEWATERFALL.

This becomes:

THECAT\start{1}ANDLION\start{2}LEFTTHE\end{2}FORESTANDMETANDOWL\start{3}INA\end{1}TREENEARTHE\end{3}WATERFALL.

Sometimes there are multiple matches in the same place:

 THECATDOESNOTLIKETALLTREES,BUTINSTEADLIKESSHORTTREES.

This becomes:

 THECAT\start{1}\start{2}DOESNOTLIKETALL\end{1}TREES,BUTINSTEADLIKESSHORT\end{2}TREES.
  • There are no spaces in the file.
  • Many non-Latin characters appear in the file.
  • Pattern matches need only be found on the same line (e.g. “CAT” on line 1 does not ever match with a “TREE” found on line 2, as those are on different lines).

How can I find these matches and mark them in this way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:04:03+00:00Added an answer on May 31, 2026 at 11:04 am

    Check this out (Ruby):

    #!/usr/bin/env ruby
    patterns = [
      ['CAT', 'TREE'],
      ['LION', 'FOREST'],
      ['OWL', 'WATERFALL']
    ]
    
    lines = [
      'THEREISACATINTHETREE.',
      'THETREEHASACAT.',
      'THECATTREEHASMANYBIRDS.',
      'THECATANDLIONLEFTTHEFORESTANDMETANDOWLINTREENEARTHEWATERFALL.',
      'THECATDOESNOTLIKETALLTREES,BUTINSTEADLIKESSHORTTREES.',
      'CAT...TREE...CAT...TREE'
    ]
    
    lines.each do |line|
      puts line
      matches = Hash.new{|h,e| h[e] = [] }
      match_indices = []
      patterns.each do |first,second|
        offset = 0
        while new_offset = line.index(first,offset) do
          # map second element of the pattern to minimal position it might be matched
          matches[second] << new_offset + first.size + 1
          offset = new_offset + 1
        end
      end
      global_counter = 1
      matches.each do |second,offsets|
        offsets.each do |offset|
          second_offset = offset
          while new_offset = line.index(second,second_offset) do
            # register the end index of the first pattern and 
            # the start index of the second pattern with the global match count
            match_indices << [offset-1,new_offset,global_counter]
            second_offset = new_offset + 1
            global_counter += 1
          end
        end
      end
      indices = Hash.new{|h,e| h[e] = ""}
      match_indices.each do |first,second,global_counter|
        # build the insertion string for the string positions the 
        # start and end tags should be placed in
        indices[first] << "\\start{#{global_counter}}"
        indices[second] << "\\end{#{global_counter}}"
      end
      inserted_length = 0
      indices.sort_by{|k,v| k}.each do |position,insert|
        # insert the tags at their positions
        line.insert(position + inserted_length,insert)
        inserted_length += insert.size
      end
      puts line
    end
    

    Result

    THEREISACATINTHETREE.
    THEREISACAT\start{1}INTHE\end{1}TREE.
    THETREEHASACAT.
    THETREEHASACAT.
    THECATTREEHASMANYBIRDS.
    THECATTREEHASMANYBIRDS.
    THECATANDLIONLEFTTHEFORESTANDMETANDOWLINTREENEARTHEWATERFALL.
    THECAT\start{1}ANDLION\start{2}LEFTTHE\end{2}FORESTANDMETANDOWL\start{3}IN\end{1}TREENEARTHE\end{3}WATERFALL.
    THECATDOESNOTLIKETALLTREES,BUTINSTEADLIKESSHORTTREES.
    THECAT\start{1}\start{2}DOESNOTLIKETALL\end{1}TREES,BUTINSTEADLIKESSHORT\end{2}TREES.
    CAT...TREE...CAT...TREE
    CAT\start{1}\start{2}...\end{1}TREE...CAT\start{3}...\end{2}\end{3}TREE
    

    EDIT

    I inserted some comments and clarified some of the variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to search a file for a word and return the whole line
I need to search a name which is stored in collection. Search criteria: eg:
For debugging purposes, I need to recursively search a directory for all files which
I need simple search-replace in doc file exported from google doc. and than upload
I need search prices which contains number 2 in start and after . 00-99,
I need to search a pretty large text file for a particular string. Its
I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount%
I need to search a string in the string array. I dont want to
I need to search across multiple columns from two tables in my database using
I need to search for all users containing a certain text string in their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.