You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

package com.yipin.liuwanr;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Mian {
public static void main(String[] args) {
//将字符串与正式表达式进行匹配
//start()返回匹配到的子字符串在字符串中的索引位置.
//end()返回匹配到的子字符串的最后一个字符在字符串中的索引位置.
//group()返回匹配到的子字符串
String[] arg = {"5","4"};//前端【输入框输入的数据】
//【代码】
String str="i = input(\"请输入\")\n" +
"b = input(\"请输入\")";
StringBuilder builder = new StringBuilder(str);
String regex = "input\\((.*?)\\)";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(builder);
int j = 0;
while (m.find()) {
if(j<arg.length){
int start = m.start();
int end = m.end();
String str2 = m.group();
String result=null;
if(str2.contains("'")){
result="'"+arg[j]+"'";
}else if(str2.contains("\"")){
result="\""+arg[j]+"\"";
}
if(!StringUtils.isEmpty(result)){
builder.replace(start,end,result);
}
m = pattern.matcher(builder);
j++;
}else{
break;
}
}
System.out.println(builder.toString());
}
}