A Function To Find Strings Between A Given Word/String

Updated sandeepparekh9 0 Tallied Votes 492 Views Share

A Function To Find Strings Between A Given Word/String

For example :
let's say you have following Text:

Title Author Product*
~~~~~ ~~~~~~ ~~~~~~~~
"Aurora's Eggs" Douglas Niles Story (Dragons 2)

The Dragons Douglas Niles Novel

The Kinslayer Wars Douglas Niles Novel

The Qualinesti Paul B. Thompson Novel
& Tonya C. Cook

Vinas Solamnus J. Robert King Novel

The Dargonesti Paul B. Thompson Novel
& Tonya C. Cook

"Storytellers" Nick O'Donohoe Story (Dragons 2)

"The Best" Margaret Weis Story (Dragons 1)

"Quarry" Adam Lesh Story (Dragons 2)

"Easy Pickings" Douglas Niles Story (Dragons 1)

The Legend of Huma Richard A. Knaak Novel

And I want to find Everthing that is in between " character.

The Answer should be like Following

Aurora's Eggs

Storytellers

The Best

Quarry

Easy Pickings


Check here for Better Understanding: [snipped]

public static List<string> FindStringBetween(string strData,string strFindWhat)
      {
          List<string> lstFound = new List<string>();
          int startIndex, EndIndex;
          startIndex = strData.IndexOf(strFindWhat);
 
          EndIndex = strData.IndexOf(strFindWhat, startIndex + strFindWhat.Length);
 
          if (EndIndex > 0)
          {
              lstFound.Add(strData.Substring(startIndex+strFindWhat.Length ,EndIndex - startIndex-strFindWhat.Length )); 
          }
 
          while (EndIndex > 0)
          {
              startIndex = strData.IndexOf(strFindWhat, EndIndex + 1);
              if (startIndex  == -1) {
                  return lstFound; }
                  EndIndex = strData.IndexOf(strFindWhat, startIndex + 1);
              if (EndIndex > 0)
              {
                  lstFound.Add(strData.Substring(startIndex + strFindWhat.Length, EndIndex - startIndex - strFindWhat.Length)); 
              }
          }
 
          return lstFound;
      }

Dani AI

Generated

Nice attempt, — the IndexOf loop will work for simple, well‑formed input but gets brittle once quotes are escaped, missing, or span lines. As suggested, a short regular expression in JavaScript is cleaner and easier to maintain.

// modern (matchAll)
const results = [...text.matchAll(/"((?:\\.|[^"\\])*)"/g)].map(m => m[1]);

// fallback (exec loop)
const re = /"((?:\\.|[^"\\])*)"/g;
const out = [];
let m;
while ((m = re.exec(text)) !== null) out.push(m[1]);

Pattern notes: the regex matches a literal double quote, then captures any sequence of either an escaped character (\\.) or any char except " or \, and finally the closing quote. That preserves apostrophes inside titles, allows escaped double quotes (like \"), and will capture across lines because the character class includes newlines. It will ignore unmatched/odd quotes (no dangling capture).

Quick tips: trim results with results.map(s => s.trim()). To unescape interior sequences (so \" becomes " or \n becomes newline) you can cautiously use JSON parsing on each capture inside a try/catch, but only if the captured content is valid as a JSON string. If the source may contain “smart” or non‑ASCII quotes from editors, normalize them to " before matching. For very large files, a streaming/indexOf approach can be more memory efficient than holding all matches in memory.

samueal 0 Junior Poster

My opinion is
Using Regex will make it simply..

sandeepparekh9 109 Posting Whiz

ya .. i know .. i made this before i learned regex.
just wanted to share...
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.