我有这个输入字符串
Click ##$#here||action1#$## to do action A
and Click ##$#here||action2#$## to do action B
and Click ##$#here||action3#$## to do action C
我需要输出strnig为
Click here to do action A
and Click here to do action B
and Click here to do action C
## $#here || action1#$ ##是一个操作码模式,“here”是替换整个操作码模式的replacement_string,“action 1”是用户点击时必须执行的底层操作“here”字符串(Hyper Link)
受审
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());
}
以上代码使用
字符串设置testTVClick 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
任何帮助或想法都将受到强烈赞赏!
答案 0 :(得分:2)
以下正则表达式将会这样做:
##\$#(.*?)\|\|action\d+#\$##
您可以使用replaceAll()
方法,如下所示:
str.replaceAll("##\\$#(.*?)\\|\\|action\\d+#\\$##", "$1")
请参阅此regex101 demo。
<强>更新强>
问题是,您要在msg
的每次迭代中将整个textTV
附加到find()
。你需要逐步建立起来。
这样的事情:
Pattern p = Pattern.compile("##\\$#(.*?)\\|\\|(.*?)#\\$##");
Matcher m = p.matcher(msg);
int prevEnd;
for (prevEnd = 0; m.find(); prevEnd = m.end()) {
String plainText = msg.substring(prevEnd, m.start());
String linkText = m.group(1);
String action = m.group(2);
SpannableString ss = new SpannableString(plainText + linkText);
ClickableSpan clickableSpan = new ClickableSpan() {
// code here
};
ss.setSpan(clickableSpan, plainText.length(), ss.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
testTV.append(ss);
testTV.setMovementMethod(LinkMovementMethod.getInstance());
}
if (prevEnd < msg.length()) {
testTV.append(msg.substring(prevEnd));
}
答案 1 :(得分:0)
您可以尝试在语句后面的字符串msg中分配更新的值,如package javaselenium;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
*
* @author Muhammad USman
*/
public class JavaSelenium {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Hello Wrold");
GetUrls("Selenium");
}
public static ArrayList GetUrls(String keyWord)
{
FirefoxDriver driver;
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.get("https://www.google.com/?gws_rd=ssl");
WebElement qElement;
qElement = driver.findElement(By.name("q"));
qElement.sendKeys(keyWord);
qElement.sendKeys(Keys.ENTER);
try {
Thread.sleep(3*1000);
} catch (InterruptedException ex) {
Logger.getLogger(JavaSelenium.class.getName()).log(Level.SEVERE, null, ex);
}
//give number how mange pages you want to crawl
int nPageToCrawl=5;
List links;
links=new ArrayList();
for (int i = 0; i < nPageToCrawl; i++) {
if(i>0)
{
System.out.println("***Click on Next Page***");
//Click on Next Page
driver.findElement(By.id("pnnext")).click();
try {
Thread.sleep(3*1000);
} catch (InterruptedException ex) {
Logger.getLogger(JavaSelenium.class.getName()).log(Level.SEVERE, null, ex);
}
}
//it will get all links of pageLinks
List<WebElement> pageLinks = driver.findElements(By.cssSelector(".r>a"));
for (WebElement pageLink : pageLinks) {
links.add(pageLink.getAttribute("href"));
System.out.println(pageLink.getText());
System.out.println(pageLink.getAttribute("href"));
}
}
//close browser
driver.quit();
return (ArrayList) links;
}
,
msg = display;
答案 2 :(得分:0)
经过一个小时的试验,我发现工作解决方案是
String regexString = Pattern.quote(pattern1) + "(.*?)" + "\\|\\|" + "(.*?)" + Pattern.quote(pattern2);
Pattern pattern = Pattern.compile(regexString);
Matcher matcher = pattern.matcher(msg);
while (matcher.find()) {
String replace = matcher.group(1); //Replacement string
String action = matcher.group(2); //Action String
msg = msg.replaceFirst(regexString, replace);
}
Log.e ("Display Message", msg);