Java 如何使用正则表达式替换字符串
在java语言中 如何使用正则表达式处理字符串Java 如何使用正则表达式替换字符串,java,regex,string,Java,Regex,String,在java语言中 如何使用正则表达式处理字符串 $.store.book[Random(1,9)].title 并将Random(1,9)的部分替换为1到9之间的真实随机数 因此,基本上,结果字符串类似于$.store.book[3]。title或$.store.book[5]。title 有人能帮我吗 您必须分三步执行此操作 首先,您必须找到“Random(1,9)”字符串。您可以使用带有捕获组的正则表达式来解析值范围。看 接下来,您必须生成随机数 最后,您可以使用生成的数字替换字符串 如果
$.store.book[Random(1,9)].title
并将Random(1,9)
的部分替换为1
到9
之间的真实随机数
因此,基本上,结果字符串类似于$.store.book[3]。title
或$.store.book[5]。title
有人能帮我吗 您必须分三步执行此操作 首先,您必须找到“Random(1,9)”字符串。您可以使用带有捕获组的正则表达式来解析值范围。看 接下来,您必须生成随机数 最后,您可以使用生成的数字替换字符串 如果要支持每个字符串多次出现,请重复该操作,直到没有出现为止
编辑:也就是说,如果您的范围始终为1到9,那么JLEW071的答案就足够简单了。我能想到的最简单的方法就是使用String.replace(): 样本输出为:
$.store.book[7].title
尝试使用
String.replace()代码>
符合您要求的示例:
String data = "$.store.book[Random(1,9)].title";
Random random = new Random();
// Replace the sub-string
String replacedData = data.replace("Random(1,9)", String.valueOf(random.nextInt() / 10000));
System.out.println(replacedData); // after replacement
使用正则表达式捕获任意数字可能是(请参见此处的):
作为一种实用方法:
对于下面的代码
public static String replaceRandom(String input) {
Pattern p = Pattern.compile("(?<=\\[)Random\\((\\d+),(\\d+)\\)(?=\\])");
Matcher m = p.matcher(input);
String output = input;
if (m.find()) {
int min = Integer.valueOf(m.group(1));
int max = Integer.valueOf(m.group(2));
int rand = min + (int)(Math.random() * ((max - min) + 1));
output = output.substring(0, m.start()) +rand+ output.substring(m.end());
}
return output;
}
public static void main(String[] args) {
System.out.println("(1,9): "
+ replaceRandom("$.store.book[Random(1,9)].title"));
System.out.println("(1,999): "
+ replaceRandom("$.store.book[Random(1,999)].title"));
System.out.println("(50,200): "
+ replaceRandom("$.store.book[Random(50,200)].title"));
}
我可能把正则表达式搞砸了。。。我看到acdcjunior刚刚发布了所有内容,并提供了一个在线IDE来验证。所以不管怎样,我都会发布我的答案,让人们欣赏我的努力!但是他的答案肯定是没有错误的,并且是相同的:)然后,我的答案会重复整个字符串的替换,正如其他答案所建议的那样。我在下面给了你一些提示,但是请发布你到目前为止的代码,描述你遇到的问题,并告诉我们你是如何尝试解决它的。你能随机吗(1,9)是其他范围吗?如果是的话,我错过了那部分…谢谢你的帮助,但我需要说随机数(1,9)并不是一成不变的,字符串应该像“$.store.book[Random(a,b)].title”,其中a和b每次都是不同的整数。明白了,下次请尽量弄清楚!在这种情况下,我认为@acdcjunior有你需要的:)
String input= "$.store.book[Random(1,9)].title";
System.out.println("Input: "+ input);
Pattern p = Pattern.compile("(?<=\\[)Random\\((\\d+),(\\d+)\\)(?=\\])");
Matcher m = p.matcher(input);
String output = input;
if(m.find()) {
int min = Integer.valueOf(m.group(1));
int max = Integer.valueOf(m.group(2));
int rand = min + (int)(Math.random() * ((max - min) + 1));
output = output.substring(0, m.start()) + rand + output.substring(m.end());
}
System.out.println("Output: "+ output );
Input: $.store.book[Random(1,9)].title
Output: $.store.book[6].title
public static String replaceRandom(String input) {
Pattern p = Pattern.compile("(?<=\\[)Random\\((\\d+),(\\d+)\\)(?=\\])");
Matcher m = p.matcher(input);
String output = input;
if (m.find()) {
int min = Integer.valueOf(m.group(1));
int max = Integer.valueOf(m.group(2));
int rand = min + (int)(Math.random() * ((max - min) + 1));
output = output.substring(0, m.start()) +rand+ output.substring(m.end());
}
return output;
}
public static void main(String[] args) {
System.out.println("(1,9): "
+ replaceRandom("$.store.book[Random(1,9)].title"));
System.out.println("(1,999): "
+ replaceRandom("$.store.book[Random(1,999)].title"));
System.out.println("(50,200): "
+ replaceRandom("$.store.book[Random(50,200)].title"));
}
(1,9): $.store.book[4].title
(1,999): $.store.book[247].title
(50,200): $.store.book[71].title
Random rnd = new Random(System.currentTimeMillis())
Pattern pattern = Pattern.compile(".*Random\\((\\d+),(\\d+)\\).*");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
int min = matcher.group(1);
int max = matcher.group(2);
int newInt = rnd.nextInt(max-min+1) + min;
str = str.replaceFirst("Random\\([^)]+\\)",String.valueOf(newInt));
matcher = pattern.matcher(str);
}