【问题标题】:Writing a regular expression to capture signed integers编写正则表达式来捕获有符号整数
【发布时间】:2015-03-13 06:45:40
【问题描述】:

我需要解析一个包含格式为该示例的整数的文件(用于对 DPLL 算法进行基准测试):

-486 535 0
-563 745 0
125 -430 512 -4 512 -4 0
512 -4 0
667 -19 0
40 -281 512 -4 0
-231 637 0

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

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

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

545 -565 7 55 0

我想捕获这些数字中的每一个。

  • 545 将是第一个
  • -565 秒
  • 7第三
  • 55第四

而 0 用于分隔这些数字

谁能给我使用java的正则表达式来做到这一点?

我使用的代码是:

                    Pattern pattern = Pattern.compile("(\\-?\\d*)\\s*(\\-?\\d*)\\s*(\\-?\\d*)\\s*0");
                Matcher matcher = pattern.matcher(sCurrentLine);
                //System.out.print("hou find one");
                if (matcher.find()) {
                    int id;
                    boolean val;
                    int i=1;
                    Clause tempClause = new Clause(counter);
                    do
                    {
                        id = Integer.parseInt(matcher.group(i));
                        val = id>0;
                        if (val == false)id*=-1;
                        tempClause.addLiteral(new Literal (id,val));

                        System.out.print("id "+id+" the current i:"+i+".\n");
                        i++;
                    }
                    while (i<3);
                this.clauses.add(tempClause);
                counter++;
                System.out.print("the i:"+i+"\n");
                }

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

【问题讨论】:

  • 是否需要使用正则表达式?
  • 正则表达式就像-?\d+一样简单。
  • 我将编辑帖子以向您展示我正在使用的代码
  • 使用两个步骤。一个正则表达式来获取整数行 (?s)\s*(.+?)\s+0\b 然后在空格上拆分 \s+
  • 你能用Scanner吗?我认为hasNextInt()nextInt() 非常适合这个。

标签: java regex matcher


【解决方案1】:

您可以使用Scanner

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

data.txt

-486 535 0
-563 745 0
125 -430 512 -4 512 -40
512 -4 0
667 -19 0
40 -281 512 -4 0
-231 637 0

输出

[-486, 535, -563, 745, 125, -430, 512, -4, 512, -40, 512, -4, 667, -19, 40, -281, 512, -4, -231, 637]

【讨论】:

  • 我需要获取每一行的整数,字符0在行尾。
  • @DavidWallace,是的,我忘了负数 :)
  • 感谢您的帮助,非常感谢:D。
【解决方案2】:

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

import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class testRegex {


    @Test
    public void testIntRegex() {

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

        String example = "545 -565 7 55 0";

        Matcher matcher = intsOnly.matcher(example);
        while (matcher.find()) {
          System.out.println(matcher.group() + " -- ");

        }
    }

}

【讨论】:

  • 是的,如果这些中的任何一个可以嵌入 alpha 字符之间,OP 似乎并不清楚。如果是这样,那么你就是要走的路。+1
【解决方案3】:

这可能相当简单。

遍历匹配此正则表达式的文件。
每次获取捕获组 1 的内容并
空格分割(使用"\\s+"

 # "(?s)\\s*(.+?)\\s+0\\b"

 (?s)
 \s* 
 ( .+? )                       # (1)
 \s+ 0 \b 

输出:

 **  Grp 0 -  ( pos 0 , len 10 ) 
-486 535 0  
 **  Grp 1 -  ( pos 0 , len 8 ) 
-486 535  
----------------
 **  Grp 0 -  ( pos 10 , len 12 ) 

-563 745 0  
 **  Grp 1 -  ( pos 12 , len 8 ) 
-563 745  
----------------
 **  Grp 0 -  ( pos 22 , len 35 ) 

125 -430 512 -4 512 -40
512 -4 0  
 **  Grp 1 -  ( pos 24 , len 31 ) 
125 -430 512 -4 512 -40
512 -4  
----------------
 **  Grp 0 -  ( pos 57 , len 11 ) 

667 -19 0  
 **  Grp 1 -  ( pos 59 , len 7 ) 
667 -19  
----------------
 **  Grp 0 -  ( pos 68 , len 18 ) 

40 -281 512 -4 0  
 **  Grp 1 -  ( pos 70 , len 14 ) 
40 -281 512 -4  
----------------
 **  Grp 0 -  ( pos 86 , len 12 ) 

-231 637 0  
 **  Grp 1 -  ( pos 88 , len 8 ) 
-231 637  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 2013-10-31
    相关资源
    最近更新 更多