簡體   English   中英

整個字符串的正則表達式

[英]regular expression for the whole string

下面的代碼內容將解析(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);
}

如果您真的想以最快的方式接收這八個值,請在注釋中使用建議。 用空字符串替換括號。 然后用逗號將整個String分割,然后將值轉換為整數。

我為您運行了一個基准,以向您展示每種方法的速度:

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);
  }
}

為此,您需要按照正則表達式模式創建所有8個組,就像:

Pattern pattern = Pattern.compile("\\((-?\\d+),(-?\\d+)\\),\\((-?\\d+),(-?\\d+)\\),\\\((-?\\d+),(-?\\d+)\\),\\((-?\\d+),(-?\\d+)\\)");

在這種情況下,您可以調用一個find()並從頭到尾獲取所有組。 但是,閱讀這樣的代碼似乎有點困難。 如果雙打的數目會隨着時間改變,這將不起作用。

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
import java.util.List;


public class GetNumbers {
    public static void main(String[] args) {

        String s1 = "(8,0),(0,-1),(7,-2),(1,1)";
        String d2 = "[-]?\\d";
        List<String> allMatches = new ArrayList<String>();
        Matcher m = Pattern.compile(d2).matcher(s1);
        while (m.find()) {
            allMatches.add(m.group());
        }

        for (String str : allMatches){
             System.out.println(str);
        }
    }
}

產量

8
0
0
-1
7
-2
1
1

Process finished with exit code 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM