Hi

I want to replace "ta,at,an,bc" in main string {ta,at,an,bc,ta,at,an,bc,ta,at,an,bc,ef,ta,at,an,bc,ta,at,an,bc" with (ta at an bc)*.The result array should be "(ta at an bc)*,e,(ta at an bc)*". Can anyone give me some hint? Thanks in advance.

Dani AI

Generated

— the task is to collapse any contiguous run of the sequence ta,at,an,bc into a single marker (ta at an bc)*, leaving other tokens alone. The example in the first post looks like it contains ef but the expected output shows e — that is probably a typo; the examples below preserve whatever token appears between the runs (so ef stays ef).

Idea (simple and robust): split the string on commas into tokens, scan left-to-right, and whenever you see the first element of the pattern check whether the full pattern matches. If it does, consume as many contiguous copies of that full pattern as you find and append the single marker. Otherwise append the current token and advance by one. That mirrors ’s “counter” idea and gives deterministic behavior for partial or overlapping matches.

Java (loop approach):

String s = "ta,at,an,bc,ta,at,an,bc,ta,at,an,bc,ef,ta,at,an,bc,ta,at,an,bc";
String[] tokens = s.split(",");
String[] pattern = {"ta","at","an","bc"};

List<String> out = new ArrayList<>();
int i = 0;
while (i < tokens.length) {
    boolean match = i + pattern.length <= tokens.length;
    if (match) for (int j = 0; j < pattern.length && match; j++) if (!tokens[i+j].equals(pattern[j])) match = false;
    if (match) {
        while (i + pattern.length <= tokens.length) {
            boolean block = true;
            for (int j = 0; j < pattern.length; j++) if (!tokens[i+j].equals(pattern[j])) { block = false; break; }
            if (!block) break;
            i += pattern.length;
        }
        out.add("(ta at an bc)*");
    } else {
        out.add(tokens[i++]);
    }
}
System.out.println(out); // -> [(ta at an bc)*, ef, (ta at an bc)*]

Python (compact, regex-based — follows ’s suggestion to use a replace):

import re
s = "ta,at,an,bc,ta,at,an,bc,ta,at,an,bc,ef,ta,at,an,bc,ta,at,an,bc"
pattern = "ta,at,an,bc"
token = "(ta at an bc)*"
result = re.sub(r'(?:' + re.escape(pattern) + r')(?:,(?:' + re.escape(pattern) + r'))*', token, s)
print(result.split(','))  # -> ['(ta at an bc)*', 'ef', '(ta at an bc)*']

Notes: trim whitespace from tokens if input can contain spaces, decide whether single occurrences should be collapsed (examples above collapse any single full-pattern block), and use case-insensitive comparisons if needed.

Recommended Answers

All 3 Replies

Look at the java.lang.String class' Java API doc through google. You should find what you're looking for.

Regards,

Nate

Have a counter variable that keeps track of the current index, iterate and find some new junk.

You can use the String classes replaceAll method. Here is an example:

String str = "Hello blank";
String newStr = str.replaceAll("blank", "Jim") ;

System.out.println(newStr); //prints "Hello Jim"

For more help,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.