0

I am working on a Java program that replaces variable values in a text file.

The variables to be replaced are encapsulated as...

 side    /*{{*/ red /*TEAM}}*/ 

or

detect_range   /*{{*/ 200 /*RANGE}}*/  nm

So in the first case I want to replace the value red with another value. The second I would replace 200.

Here I am reading the file line by line looking for that pattern.

       File file = new File(currentFile);

    try {
        Scanner scanner = new Scanner(file);


        int lineNum = 0;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            lineNum++;
            if (<match regex expression for xxxxx /*{{*/ value /*VariableNAME}}*/ >) {

            }
        }
    } catch (Exception e) {
        System.out.println(e.toString());
        //handle this
    }

What is a regex expression to that I Could use to find these patterns?

Edit:

I have a line in the file that would say

side    /*{{*/ red /*TEAM}}*/ 

The output change the line in the file to

side    /*{{*/ blue /*TEAM}}*/ 

The string "TEAM" is the identifier.

4
  • the /* */ pairs, are they part of the pattern, or just comment markers? Commented Aug 11, 2014 at 17:35
  • Is TEAM and RANGE are dynamic value? Commented Aug 11, 2014 at 17:36
  • You need to specify the problem more clearly. Show some examples of actual input and the expected output and explain how the replacement values are mapped to the tags they replace. Commented Aug 11, 2014 at 17:39
  • TEAM and RANGE are are just variable names. I must retrieve the variable names to know what values to append to them. Commented Aug 11, 2014 at 18:23

2 Answers 2

1

You can use below use with String.replaceAll() method.

(?<=\/\*\{\{\*\/ ).*?(?= \/\*(TEAM|RANGE)\}\}\*\/)

Here is online demo

Note: Use \w+ if value "TEAM" and "RANGE" are dynamic.


Sample code:

String str1 = "side    /*{{*/ red /*team}}*/ ";
String str2 = "detect_range   /*{{*/ 200 /*RANGE}}*/  nm";
String pattern = "(?i)(?<=\\/\\*\\{\\{\\*\\/ ).*?(?= \\/\\*(TEAM|RANGE)\\}\\}\\*\\/)";
System.out.println(str1.replaceAll(pattern, "XXX"));
System.out.println(str2.replaceAll(pattern, "000"));

output:

side    /*{{*/ XXX /*team}}*/ 
detect_range   /*{{*/ 000 /*RANGE}}*/  nm

If you want to get "TEAM" or "RANGE" then get it from index 1.

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str1);
if (m.find()) {
    System.out.println(m.group(1));
}
Sign up to request clarification or add additional context in comments.

7 Comments

How could I extract the variable name "team" for example, to compare it to know what value to insert.
just get the matched group from index 1 as shown in demo as well. Do you need code as well?
That would be helpful.
In the pattern String pattern = "(?i)(?<=\\/\*\\{\\{\*\\/ ).*?(?= \\/\*(TEAM|RANGE)\\}\\}\*\\/)"; Could I replace TEAM|RANGE with a wildcard so that it would match no matter what the variable name is?
yes you can use it, as I mentioned use \w+ instead of (TEAM|RANGE). Are you looking for only variable names then I can optimize it, if you need?
|
0

You can use this regex:

/\*{{\*/ *(\S+) */\*[^}]*}}\*/

and grab captured group #1

RegEx Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.