Hello forum,

I am wondering if it is possible to do a mark-up in free text by comparison with some string. Suppose I have a file with several sub-strings, one in each line. I have another file which contains the full text from where the sub-strings are generated. Is it possible to mak-up the source text with <found> </found> tags?

If possible please give the code snippet for this purpose.

Thanks in advance.

Dani AI

Generated

wanted to wrap substrings (one per line in a file) inside a source text with <found>...</found> tags. pointed out inserting into a Swing Document (good for an editor UI) and suggested rebuilding the string using indexOf() (simple and clear). Both approaches work, but file-based bulk markup benefits from a safer, scalable routine that avoids manual index arithmetic and preserves match order.

A practical, robust method is to perform a single-pass search using Java regex with Matcher.appendReplacement() (or a multi-pattern search like Aho-Corasick for large lists). Read the search terms, sort them by length descending (prefer longer matches), escape each term with Pattern.quote(), build an alternation pattern, then iterate matches and wrap each occurrence. That approach builds a new output string rather than inserting in-place, so you do not need to adjust offsets as you add tags.

Example (file-to-string processing):

List<String> terms = ...; // read and sort by length desc
StringBuilder alt = new StringBuilder();
for (int i = 0; i < terms.size(); i++) {
  if (i > 0) alt.append("|");
  alt.append(Pattern.quote(terms.get(i)));
}
Pattern p = Pattern.compile(alt.toString());
Matcher m = p.matcher(sourceText);
StringBuffer out = new StringBuffer();
while (m.find()) {
  m.appendReplacement(out, "<found>" + Matcher.quoteReplacement(m.group()) + "</found>");
}
m.appendTail(out);
String result = out.toString();

Notes and pitfalls: building a giant alternation can be slow or fail for thousands of patterns; use an Aho-Corasick library (multi-pattern Trie) for large lists. Decide how to treat overlapping matches (longest-first, leftmost-first, or allow nesting) and implement that policy explicitly. If the output must be valid XML/HTML, escape special characters or insert real XML nodes via a parser/DOM rather than plain text replacement. Always test on copies, verify character encoding, and run cases with overlapping and special-character terms.

Recommended Answers

All 3 Replies

Make a document over that file, then just write:

doc.insertString(offset,"The String",null)

.

The easiest way to do :

JTextArea text = new JTextArea();
text.setText("Your string");
AbstractDocument doc = text.getDocument();
doc.insertString(offset,"The String",null)

Offset is the position from which to start.

Sounds like an exercise for indexOf() - find the string in the source, and build a new source:
"everything up to where found" + <tag> + foundString + </tag> + "everything after the end of the found string"

Thanks for the replies. I like NormR1's approach as I am thinking in the same lines. The process is like a "Find" and mark-up. Something more concrete will help.

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.