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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:55:46+00:00 2026-06-03T04:55:46+00:00

In a Java program, I want to find out all the occurrences in a

  • 0

In a Java program, I want to find out all the occurrences in a given String of these substrings: $$, or $\d (the symbol ‘$’ followed by an integer).

My problem started when I added an additional constraint stating that a match occurs only if the matched string is not part of a substring limited by certain sequence of characters.

For example, I want to ignore the matches if they are part of a substring surrounded by “/{” and “/}”.

The following example brings all the occurrences of $$, or $\d, but does not considere the additional constraint of ignoring the match if it is inside “/{” and “/}”.

public static final String PARAMETERS_PREFIX = "$";
public static final String ALL_PARAMS_SUFFIX = "$";
public static final String BEGIN_JAVA_EXPRESSION = "/{";
public static final String END_JAVA_EXPRESSION = "/}";
...
String test = "$1 xxx $$ " //$1 and $$ are matches
  + BEGIN_JAVA_EXPRESSION + "xxx $2 xxx" + END_JAVA_EXPRESSION; //$2 SHOULD NOT be a match
Set<String> symbolsSet = new LinkedHashSet<String>();
Pattern pattern = Pattern.compile(Pattern.quote(PARAMETERS_PREFIX)+"(\\d+|"+Pattern.quote(ALL_PARAMS_SUFFIX)+")");
Matcher findingMatcher = pattern.matcher(test);
while(findingMatcher.find()) {
  String match = findingMatcher.group();
  symbolsSet.add(match);
}
return new ArrayList<String>(symbolsSet);

In addition to find the keywords that are not part of certain substring, I want to be able to replace afterwards only those keywords by certain values.
Then, the option of just removing everything between the delimited characters before doing the match probably will not help, since afterwards I need to be able to get the original string with the matched tokens replaced by certain values, and the tokens inside the delimited region should be left without modifications.
This should be easy if I found the right regex.

Does someone could give me a hint about how to write the right regex for this problem ?.

  • 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-06-03T04:55:47+00:00Added an answer on June 3, 2026 at 4:55 am

    Is it permissible to use more than 1 regex? It might be less sexy, but you could do this with 3 regex’s pretty easily.
    (these are not the actual regex’s)
    1. For getting the string you are looking for ($$ | ${num})
    2. For ‘/{‘
    3. For ‘/}’

    It should be fairly easy to match up the invalid areas in 2 and 3. You can then use those spans to eliminate results in 1

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

Sidebar

Related Questions

Is there anyway in Java to find out if the given path is absolute
I am new to Java. My Problem is that my Java program cannot find
I want to write a java program that acts as a user interface to
I want to create registry key through java program to add the jar file
I want to start my successfully installed Java program after the installation finished. I
I want to create a simple bash script to launch a Java program on
I want run a script as follows: runner: ssh 'java program &' ssh 'java
I want to execute a batch file from a java program. I am using
I want to know how I can make a Java program where an unknown
I want to increase my CPU usage through a java program, albeit in a

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.