-2

我想将此文件输入替换为 html 表:

        ip add                  St Stat  Type Mode   ip only       class  numbers   
 ------------------------------ -- ----- ---- ---- --------------- ------ -----  
 ABC_127.562.200.5/32           -    up  ABC  -    127.562.200.5          5      
 ABC_127.292.200.3/32           -    up  ABC  -    127.562.200.5          4      
 ABC_127.262.200.13/32          -    up  ABC  -    127.562.200.5          3  
 ABC:jdnsajkds

我知道这将以“ABC”结尾,但我无法弄清楚为什么“/”也会出现在输入中

import java.util.regex.*;


interface LogExample {

    public static final int NUM_FIELDS = 7;


    public static final String logEntryLine = "ABC_127.562.200.5/32 **space**        -- **space**    up **space**  ABC **space** -- **space**    127.562.200.5 **space**         5 **space** ";

}


public class LogRegExp implements LogExample {

    public static void main(String argv[]) {

        String logEntryPattern = "";//thats i am not getting

        System.out.println("Using RE Pattern:");
        System.out.println(logEntryPattern);

        System.out.println("Input line is:");
        System.out.println(logEntryLine);

        Pattern p = Pattern.compile(logEntryPattern);
        Matcher matcher = p.matcher(logEntryLine);
        if (!matcher.matches() || 
                NUM_FIELDS != matcher.groupCount()) {
            System.err.println("Bad log entry (or problem with RE?):");
            System.err.println(logEntryLine);
            return;
        }
        System.out.println("name + IP Address: " + matcher.group(1));
        System.out.println("status1: " + matcher.group(2));
        System.out.println("status2: " + matcher.group(3));
        System.out.println("type: " + matcher.group(4));
        System.out.println("mode: " + matcher.group(5));
        System.out.println("IP Address: " + matcher.group(6));
        System.out.println("class: " + matcher.group(7));
        System.out.println("numbers: " + matcher.group(8));

    }
}
4

2 回答 2

0

由于您的课程列是空白的,我们无法做太多事情来提取该信息。但是这个正则表达式将匹配您拥有的 7 列数据:

    String logEntryPattern = "(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)";

我们需要转义 Java 字符串文字中的反斜杠。

    System.out.println("name + IP Address: " + matcher.group(1));
    System.out.println("status1: " + matcher.group(2));
    System.out.println("status2: " + matcher.group(3));
    System.out.println("type: " + matcher.group(4));
    System.out.println("mode: " + matcher.group(5));
    System.out.println("IP Address: " + matcher.group(6));
    System.out.println("numbers: " + matcher.group(7));

坦率地说,正则表达式对于您想要做的事情有点多 - 只是对空格进行标记也可以 - 但它可以完成工作。

于 2012-05-22T22:08:17.187 回答
0

我得到了解决方案:

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

public class RegexMatches
{
    public static void main( String args[] )
    {
        // String to be scanned to find the pattern.
        // String line = "MSEM-E-031_TO_RVBC-R-001_T1    en   up  TE   ingr 124.252.200.2   ELSP   0";
        // String pattern = "((\\-{8})+.*?)";
        String line = "ADR-SSF-1008-M008              vlan  en dn 10081008";
        String pattern = "((\\-{6})+.*?)";

        // Create a Pattern object
        Pattern r = Pattern.compile(pattern);

        // Now create matcher object.
        Matcher m = r.matcher(line);
        if (m.find( ))
        {
            System.out.println("Found value: " + m.group(0) );
            System.out.println("Found value: " + m.group(1) );
            System.out.println("Found value: " + m.group(2) );
        }
        else
        {
            System.out.println("NO MATCH");
        }
    }
}
于 2012-05-27T10:52:12.123 回答