java—编写正则表达式以捕获有符号整数

dz6r00yl  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(336)

我需要解析一个包含整数的文件,格式如下例所示(用于dpll算法的基准测试):

  1. -486 535 0
  2. -563 745 0
  3. 125 -430 512 -4 512 -4 0
  4. 512 -4 0
  5. 667 -19 0
  6. 40 -281 512 -4 0
  7. -231 637 0

一般来说,数据的格式是这样的

  1. number number number 0
  2. numbers are separated by space and each line ends with the character 0,

例如,这可能是我想要解析的字符串

  1. 545 -565 7 55 0

我要捕捉这些数字。
545是第一个
-565秒
7第三
55第四
0表示分隔这些数字
有没有人能给我一个正则表达式,让我用java来实现这一点?
我使用的代码是:

  1. Pattern pattern = Pattern.compile("(\\-?\\d*)\\s*(\\-?\\d*)\\s*(\\-?\\d*)\\s*0");
  2. Matcher matcher = pattern.matcher(sCurrentLine);
  3. //System.out.print("hou find one");
  4. if (matcher.find()) {
  5. int id;
  6. boolean val;
  7. int i=1;
  8. Clause tempClause = new Clause(counter);
  9. do
  10. {
  11. id = Integer.parseInt(matcher.group(i));
  12. val = id>0;
  13. if (val == false)id*=-1;
  14. tempClause.addLiteral(new Literal (id,val));
  15. System.out.print("id "+id+" the current i:"+i+".\n");
  16. i++;
  17. }
  18. while (i<3);
  19. this.clauses.add(tempClause);
  20. counter++;
  21. System.out.print("the i:"+i+"\n");
  22. }

在这段代码中我捕获了3个整数,我需要改进以捕获该字符串中的所有整数。

svgewumm

svgewumm1#

你可以用一个 Scanner :

  1. public static void main(String[] arguments) throws FileNotFoundException {
  2. Scanner scanner = new Scanner(new File("data.txt"));
  3. List<Integer> integers = new ArrayList<Integer>();
  4. while (scanner.hasNext()) {
  5. int i = scanner.nextInt();
  6. if (i != 0)
  7. integers.add(i);
  8. }
  9. System.out.println(integers);
  10. }

数据.txt

  1. -486 535 0
  2. -563 745 0
  3. 125 -430 512 -4 512 -40
  4. 512 -4 0
  5. 667 -19 0
  6. 40 -281 512 -4 0
  7. -231 637 0

输出

  1. [-486, 535, -563, 745, 125, -430, 512, -4, 512, -40, 512, -4, 667, -19, 40, -281, 512, -4, -231, 637]
展开查看全部
ds97pgxw

ds97pgxw2#

为运行上述要求而实施的测试

  1. import org.junit.Test;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. public class testRegex {
  5. @Test
  6. public void testIntRegex() {
  7. Pattern intsOnly = Pattern.compile("(-?\\d+)");
  8. String example = "545 -565 7 55 0";
  9. Matcher matcher = intsOnly.matcher(example);
  10. while (matcher.find()) {
  11. System.out.println(matcher.group() + " -- ");
  12. }
  13. }
  14. }
展开查看全部
nvbavucw

nvbavucw3#

这可能相当简单。
循环遍历与此正则表达式匹配的文件。
每次采集第1组的内容
在空白处拆分(使用 "\\s+" )

  1. # "(?s)\\s*(.+?)\\s+0\\b"
  2. (?s)
  3. \s*
  4. ( .+? ) # (1)
  5. \s+ 0 \b

输出:

  1. **Grp 0 - ( pos 0 , len 10 )
  2. -486 535 0
  3. **Grp 1 - ( pos 0 , len 8 )
  4. -486 535
  5. ----------------
  6. **Grp 0 - ( pos 10 , len 12 )
  7. -563 745 0
  8. **Grp 1 - ( pos 12 , len 8 )
  9. -563 745
  10. ----------------
  11. **Grp 0 - ( pos 22 , len 35 )
  12. 125 -430 512 -4 512 -40
  13. 512 -4 0
  14. **Grp 1 - ( pos 24 , len 31 )
  15. 125 -430 512 -4 512 -40
  16. 512 -4
  17. ----------------
  18. **Grp 0 - ( pos 57 , len 11 )
  19. 667 -19 0
  20. **Grp 1 - ( pos 59 , len 7 )
  21. 667 -19
  22. ----------------
  23. **Grp 0 - ( pos 68 , len 18 )
  24. 40 -281 512 -4 0
  25. **Grp 1 - ( pos 70 , len 14 )
  26. 40 -281 512 -4
  27. ----------------
  28. **Grp 0 - ( pos 86 , len 12 )
  29. -231 637 0
  30. **Grp 1 - ( pos 88 , len 8 )
  31. -231 637
展开查看全部

相关问题