I have this input string
Click ##$#here||action1#$## to do action A
and Click ##$#here||action2#$## to do action B
and Click ##$#here||action3#$## to do action C
And I need the output strnig as
Click here to do action A
and Click here to do action B
and Click here to do action C
##$#here||action1#$## is an opcode pattern with "here" is replacement_string that replaces the whole opcode pattern and the "action 1" is the underlying action that has to be performed when user clicks on the "here" string (Hyper Link)
TRIED
String msg =Click ##$#here||action1#$## to do action A
and Click ##$#here||action2#$## to do action B
and Click ##$#here||action3#$## to do action C;
String pattern1 = "##$#", pattern2 = "#$##";
String regexString = Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2);
Pattern pattern = Pattern.compile(regexString);
Matcher matcher = pattern.matcher(msg);
while (matcher.find()) {
String action = matcher.group(1);
final String replaceAction[] = action.split("\\|\\|");
String display = msg.replace(pattern1 + action + pattern2, replaceAction[0]);
SpannableString ss = new SpannableString(display);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
Log.e("CLICKED", "CLICKED");
Log.e("Action Intent", replaceAction[1]);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(true);
}
};
ss.setSpan(clickableSpan, display.indexOf(replaceAction[0]),
display.indexOf(replaceAction[0]) + replaceAction[0].length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
testTV.append(ss);
testTV.setMovementMethod(LinkMovementMethod.getInstance());
}
The above code sets the testTV with the string of
Click here to do action A
and Click ##$#here||action2#$## to do action B
and Click ##$#here||action3#$## to do action C
Click ##$#here||action1#$## to do action A
and Click here to do action B
and Click ##$#here||action3#$## to do action C
Click ##$#here||action1#$## to do action A
and Click ##$#here||action2#$## to do action B
and Click here to do action C
Any help or a notion would be strongly appreciated!!!
\#.+\#or\#.*\#should work for pattern matching.