-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 に答える