3

I want to use the $1 value like an integer.
The idea is to replace all numbers from originaltext with the equivalent array values and create a new text.
The below desired outcome should be "This is DBValue4, This is DBValue2, This is DBValue7"
Also, is there a way to save these backreferences for further use?

String[] values = {"DBValue0","DBValue1","DBValue2","DBValue3","DBValue4","DBValue5","DBValue6","DBValue7","DBValue8","DBValue9","DBValue10"};
String originaltext = "This is 4, This is 2, This is 7";
text = originaltext.replaceAll("(\\d)","$1");
// want something like
text = originaltext.replaceAll("(\\d)",values[$1]);
//or
text = originaltext.replaceAll("(\\d)",values[Integer.parseInt("$1")]);
2

1 Answer 1

4

You can use Pattern and Matcher like so:

public static void main(String[] args) throws Exception {
    final String[] values = {"DBValue0", "DBValue1", "DBValue2", "DBValue3", "DBValue4", "DBValue5", "DBValue6", "DBValue7", "DBValue8", "DBValue9", "DBValue10"};
    final String originaltext = "This is 4, This is 2, This is 7";
    final Pattern pattern = Pattern.compile("(?<=This is )\\d++");
    final Matcher matcher = pattern.matcher(originaltext);
    final StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        System.out.println(matcher.group());
        final int index = Integer.parseInt(matcher.group());
        matcher.appendReplacement(sb, values[index]);
    }
    matcher.appendTail(sb);
    System.out.println(sb);
}

Output:

4
2
7
This is DBValue4, This is DBValue2, This is DBValue7

EDIT

Further to the OP's comment is seems that the OP needs to replace Strings of the form {name, index} where "name" is the name of an array and "index" is the index of an element in that array.

This is easily achieved by Mapping the arrays to their names using a Map<String, String[]> and then using a Pattern that captures first the name then the index.

public static void main(String[] args) throws Exception {
    final String[] companies = {"Company1", "Company2", "Company3"};
    final String[] names = {"Alice", "Bob", "Eve"};
    final String originaltext = "This is {company, 0}, This is {name, 1}, This is {name, 2}";
    final Map<String, String[]> values = new HashMap<>();
    values.put("company", companies);
    values.put("name", names);
    final Pattern pattern = Pattern.compile("\\{([^,]++),\\s*+(\\d++)}");
    final Matcher matcher = pattern.matcher(originaltext);
    final StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
        final int index = Integer.parseInt(matcher.group(2));
        matcher.appendReplacement(sb, values.get(matcher.group(1))[index]);
    }
    matcher.appendTail(sb);
    System.out.println(sb);
}

Output:

company
0
name
1
name
2
This is Company1, This is Bob, This is Eve
Sign up to request clarification or add additional context in comments.

4 Comments

I'm sorry but for reasons of simplicity, the example is not so good. What you have done, works but I want to use something more complicated like " String originaltext = "{company,3} sents this message for {name,2} owner of the number {number,10} from {new,11}"; " and " text = originaltext.replaceAll("\\{.+?,(\\d\\d?)\\}",values[Integer.parseInt("$1")]); ". Any suggestions?
I don't understand - so you have several arrays and you want to select between them on a String?
No, I just want to replace {something,an_int} in any text with the values[an_int] array property.
My edit should do that for you if you make a couple of changes.

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.