Gandalf StormCrow
Gandalf StormCrow

Reputation: 26212

Java regex matching

strong textI have a bunch of lines in a textfile and I want to match this ${ALPANUMERIC characters} and replace it with ${SAME ALPHANUMERIC characters plus _SOMETEXT(CONSTANT)}.

I've tried this expression ${(.+)} but it didn't work and I also don't know how to do the replace regex in java.

thank you for your feedback

Here is some of my code :

BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        StringBuilder sb = new StringBuilder();

        while ((line = br.readLine()) != null) {
            Pattern p = Pattern.compile("\\$\\{.+\\}");
            Matcher m = p.matcher(line); // get a matcher object
               if(m.find()) {
                   System.out.println("MATCH: "+m.group());
                   //TODO
                   //REPLACE STRING
                   //THEN APPEND String Builder
               }
            }               

OK this above works but it only founds my variable and not the whole line for ex here is my input :

some text before ${VARIABLE_NAME} some text after
some text before ${VARIABLE_NAME2} some text after
some text before some text without variable some text after
... etc

so I just want to replace the ${VARIABLE_NAME} or ${VARIABLE_NAME} with ${VARIABLE_NAME2_SOMETHING} but leave preceding and following text line as it is

EDIT:

I though I though of a way like this :

if(line.contains("\\${([a-zA-Z0-9 ]+)}")){
                    System.out.println(line);
                }

if(line.contains("\\$\\{.+\\}")){
                    System.out.println(line);
                }

My idea was to capture the line containing this, then replace , but the regex is not ok, it works with pattern/matcher combination though.

EDIT II

I feel like I'm getting closer to the solution here, here is what I've come up with so far :

  if(line.contains("$")){
                    System.out.println(line.replaceAll("\\$\\{.+\\}", "$1" +"_SUFFIX"));
                }

What I meant by $1 is the string you just matched replace it with itself + _SUFFIX

Upvotes: 1

Views: 6260

Answers (4)

Alan Moore
Alan Moore

Reputation: 75252

line = line.replaceAll("\\$\\{\\w+", "$0_SOMETHING");

There's no need to check for the presence of $ or whatever; that's part of what replaceAll() does. Anyway, contains() is not regex-powered like find(); it just does a plain literal text search.

Upvotes: 1

Gandalf StormCrow
Gandalf StormCrow

Reputation: 26212

THE SOLUTION :

while ((line = br.readLine()) != null) {

                if(line.contains("$")){ 
                    sb.append(line.replaceAll("\\$\\{(.+)\\}", "\\${$1" +"_SUFFIX}") + "\n");
                }else{
                    sb.append(line + "\n");
                }

            }   

Upvotes: 1

MikeD
MikeD

Reputation: 3378

The $ is a special regular expression character that represents the end of a line. You'll need to escape it in order to match it. You'll also need to escape the backslash that you use for escaping the dollar sign because of the way Java handles strings.

Once you have your text in a string, you should be able to do the following:

str.replaceAll("\\${([a-zA-Z0-9 ]+)}", "\\${$1 _SOMETEXT(CONSTANT)}")

If you have other characters in your variable names (i.e. underscores, symbols, etc...) then just add them to the character class that you are matching for.

Edit: If you want to use a Pattern and Matcher then there are still a few changes. First, you probably want to compile your Pattern outside of the loop. Second, you can use this, although it is more verbose.

Pattern p = Pattern.compile("\\$\\{.+\\}");
Matcher m = p.matcher(line);
sb.append(m.replaceAll("\\${$1 _SOMETEXT(CONSTANT)}"));

Upvotes: 1

sigint
sigint

Reputation: 316

I would use the String.replaceAll() method like so:

`String old="some string data";

String new=old.replaceAll("$([a-zA-Z0-9]+)","(\1) CONSTANT"); `

Upvotes: 1

Related Questions