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 996441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:52:36+00:00 2026-05-16T06:52:36+00:00

My tool gets a plain text and gradually generates the tags by replacing a

  • 0

My tool gets a plain text and gradually generates the “tags” by replacing a terms from text in tags. Due to existence of some compound terms, the only way (i think) is use ReplaceAll regex.

Thanks to the friends of stackoverflow, in my last question i got a excellent regex to my app, but after a tests, emerged a new need:

“A regex to replace all word OUTSIDE a tag AND outside another word”

The orginal code:

String str = "world worldwide <a href=\"world\">my world</world>underworld world";
str = str.replaceAll("\\bworld\\b(?![^<>]*+>)", "repl");
System.out.println(str);

I need now replace only “world” (outside a tag ofcourse) and NOT “underworld” or “worldwide”

Expected result:

repl worldwide <a href="world">my world</world>underworld repl
  • 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-16T06:52:37+00:00Added an answer on May 16, 2026 at 6:52 am

    I don’t think regex is the best tool for the job, but if you just want to tweak and optimize what you have right now, you can use the word boundary \b, throw away the unnecessary capturing group and optional repetition specifier, and use possessive repetition:

    \bworld\b(?![^<>]*+>)
    

    The \bworld\b will ensure that "world" are surrounded by the zero-width word boundary anchors. This will prevent it from matching the "world" in "underworld" and "worldwide". Do note that the word boundary definition may not be exactly what you want, e.g. \bworld\b will not match the "world" in "a_world_domination".

    The original pattern also contains a subpattern that looks like (x+)?. This is probably better formulated as simply x*. That is, instead of “zero-or-one” ? of “one-or-more” +, simply “zero-or-more” *.

    The capturing group (…) is functionally not needed, and it doesn’t seem like you need the capture for any substitution in the replacement, so getting rid of it can improve performance (when you need the grouping aspect, but not the capturing aspect, you can use non-capturing group (?:…) instead).

    Note also that instead of [^<], we now forbid both brackets with [^<>]. Now the repetition can be specified as possessive since no backtracking is required in this case.

    (The […] is a character class. Something like [aeiou] matches one of any of the lowercase vowels. [^…] is a negated character class. [^aeiou] matches one of anything but the lowercase vowels.)

    Of course (?!…) is negative lookahead; it asserts that a given pattern can NOT be matched. So the overall pattern reads like this:

    \bworld\b(?![^<>]*+>)
    \_______/\__________/ NOT the case that
     "world"                      the first bracket to its right is a closing one
     surrounded by
     word boundary anchors
    

    References

    • regular-expressions.info/Word Boundaries, Brackets for Grouping, Repetition, Possessive, Lookarounds

    Note that to get a backslash in a Java string literal, you need to double it, so the whole pattern as a Java string literal is "\\bworld\\b(?![^<>]*+>)".

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

Sidebar

Related Questions

I have an application that gets terms from a DB to run as a
I have the following situation: There is a tool that gets an XSLT from
What tool do you use to only zip up the source code of an
Modeling tool or just a visual tool for drawing. In fact I only need
Let's say I'm writing a Java backend for some front-end GUI (Swing) tool. This
Is there some plugin or tool which can read a .h file (or simply
I've got a WCF host application which gets started by an executable with some
I'm writing some sort of backup tool that has to copy all the files
I'm working on a tool which connects to a SQL Database, gets back a
I'm looking for a utility or library for extracting text from PDFs and formatting

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.