gpt4 book ai didi

java - 整个字符串的正则表达式

转载 作者:行者123 更新时间:2023-11-29 03:04:59 24 4
gpt4 key购买 nike

下面的代码将解析 (8,0),(0,-1),(7,-2),(1,1) 字符串并在迭代时显示值。代码运行良好,但我的想法是我们可以为整个字符串编写正则表达式吗?我们能否获得像 matcher.group(1)、matcher.group(2)、matcher.group(3) 这样的值? matcher.group(4), matcher.group(5), matcher.group(6), matcher.group(7), matcher.group(8)

我的代码如下

Pattern pattern = Pattern.compile("\\((-?\\d+),(-?\\d+)\\)");
Matcher matcher = pattern.matcher("(8,0),(0,-1),(7,-2),(1,1)");

while (matcher.find()) {
int x = Integer.parseInt(matcher.group(1));
int y = Integer.parseInt(matcher.group(2));
System.out.printf("x=%d, y=%d\n", x, y);
}

最佳答案

如果您真的想以最快的方式接收八个值,请使用评论中的建议。用空字符串替换括号。然后用逗号分隔整个字符串并将值转换为整数。

我为您运行了一个基准测试来向您展示每种方法的速度:

Benchmark                     Mode  Cnt     Score    Error  Units
MyBenchmark.testRegexLoop avgt 30 1232,524 ± 42,972 ns/op
MyBenchmark.testRegexWhole avgt 30 2638,561 ± 59,419 ns/op
MyBenchmark.testReplaceSplit avgt 30 1045,388 ± 66,791 ns/op

重现结果:

import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

@Fork(3)
@BenchmarkMode(Mode.AverageTime)
@Measurement(iterations = 10, timeUnit = TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
@Threads(1)
@Warmup(iterations = 5, timeUnit = TimeUnit.NANOSECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class MyBenchmark {

Blackhole bh = new Blackhole();

@Benchmark
public void testRegexLoop() {
Pattern pattern = Pattern.compile("\\((-?\\d+),(-?\\d+)\\)");
Matcher matcher = pattern.matcher("(8,0),(0,-1),(7,-2),(1,1)");

while (matcher.find()) {
int x = Integer.parseInt(matcher.group(1));
int y = Integer.parseInt(matcher.group(2));
bh.consume(x);
bh.consume(y);
}
}

@Benchmark
public void testRegexWhole() {
Pattern pattern = Pattern
.compile("\\((-?\\d+),(-?\\d+)\\),\\((-?\\d+),(-?\\d+)\\),\\((-?\\d+),(-?\\d+)\\),\\((-?\\d+),(-?\\d+)\\)");
Matcher matcher = pattern.matcher("(8,0),(0,-1),(7,-2),(1,1)");
matcher.find();
bh.consume(Integer.parseInt(matcher.group(1)));
bh.consume(Integer.parseInt(matcher.group(2)));
bh.consume(Integer.parseInt(matcher.group(3)));
bh.consume(Integer.parseInt(matcher.group(4)));
bh.consume(Integer.parseInt(matcher.group(5)));
bh.consume(Integer.parseInt(matcher.group(6)));
bh.consume(Integer.parseInt(matcher.group(7)));
bh.consume(Integer.parseInt(matcher.group(8)));
}

@Benchmark
public void testReplaceSplit() {
String s = "(8,0),(0,-1),(7,-2),(1,1)";
String[] values = s.replaceAll("[()]", "").split(",");
int[] intValues = new int[values.length];
for (int i = 0; i < values.length; i++) {
intValues[i] = Integer.parseInt(values[i]);
}
bh.consume(intValues);
}
}

关于java - 整个字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32577561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com