divz
divz

Reputation: 7957

Regex to get particular values from a string data

While executing a particular shell command am getting following output as follows and keeping this in a string variable

dat /var/france.log

exit


bluetooth_mac=45h6kuu
franceIP=testMarmiton
build_type=BFD
france_mac=F4:0E:83:35:E8:D1
seloger_mac=F4:0E:83:35:E8:D0
tdVersion=1.2
td_number=45G67j
france_mac=fjdjjjgj
logo_mac=tiuyiiy

logout
Connection to testMarmiton closed.

Disconnected channel and session

From this i have too fetch particular details like below and put htese values in a Map. How i can perform this using java.

bluetooth_mac=45h6kuu
build_type=BFD
tdVersion=1.2
seloger_mac=F4:0E:83:35:E8:D0
france_mac=fjdjjjgj

Map<String, String> details =new HashMap<String,String>();
details.put(bluetooth_mac, 45h6kuu);
details.put(build_type, BFD)
etc
etc

Upvotes: 0

Views: 67

Answers (4)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59950

Solution 1

If you are using Java 8 you can use :

String fileName = "shell.txt";
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

    Map<String, String> result = stream
            .filter(line -> line.matches("\\w+=\\w+"))
            .map(line -> line.split("="))
            .collect(Collectors.toMap(a -> a[0], a -> a[1]));

} catch (IOException e) {
    e.printStackTrace();
}

Outputs

{franceIP=testMarmiton, bluetooth_mac=45h6kuu, logo_mac=tiuyiiy, td_number=45G67j, france_mac=fjdjjjgj, build_type=BFD}

Solution 2

It seems that you have multiple line which have the same name, in this case I would like to group by a Map<String, List<String>> :

String fileName = "shell.txt";
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

    Map<String, List<String>> result = stream
            .filter(line -> line.matches("[^=]+=[^=]+")) // filter only the lines which contain one = signe
            .map(line -> line.split("=")) // split with = sign
            .collect(Collectors.groupingBy(e -> e[0], Collectors.mapping(e -> e[1], Collectors.toList())));

    result.forEach((k, v) -> System.out.println(k + " : " + v));
} catch (IOException e) {
    e.printStackTrace();
}

Outputs

franceIP : [testMarmiton]
bluetooth_mac : [45h6kuu]
logo_mac : [tiuyiiy]
td_number : [45G67j]
seloger_mac : [F4:0E:83:35:E8:D0]
france_mac : [F4:0E:83:35:E8:D1, fjdjjjgj]
tdVersion : [1.2]
build_type : [BFD]

Upvotes: 1

user8701826
user8701826

Reputation:

Here's a complete example, extracting the value using regex-matching and building a HashMap (the appropriate map for key-value pairs). You can copy the whole program and run it yourself:

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

public class main {
    public static void main(String[] args) {
        String input = // Your input log
                "dat /var/france.log\n" +
                "\n" +
                "exit\n" +
                "\n" +
                "root@france24:~# \n" +
                "root@france24:~# dat /var/france.log\n" +
                "bluetooth_mac=45h6kuu\n" +
                "franceIP=testMarmiton\n" +
                "build_type=BFD\n" +
                "france_mac=F4:0E:83:35:E8:D1\n" +
                "seloger_mac=F4:0E:83:35:E8:D0\n" +
                "tdVersion=1.2\n" +
                "td_number=45G67j\n" +
                "france_mac=fjdjjjgj\n" +
                "logo_mac=tiuyiiy\n" +
                "root@france24:~# \n" +
                "root@france24:~# exit\n" +
                "logout\n" +
                "Connection to testMarmiton closed.\n" +
                "\n" +
                "Disconnected channel and session"; 


        String[] keys = {
                "bluetooth_mac",
                "build_type",
                "tdVersion",
                "seloger_mac",
                "france_mac"
        };

        HashMap<String, String> map = new HashMap<>();
        for(String key : keys){
            String value = getValueOf(input, key);
            if(value != null)
                map.put(key, value);
        }

        for(String key : keys)
            System.out.println(key + " = " + map.get(key));
    }

    public static String getValueOf(String input, String key){ //returns null if not found
        String result = null;
        Pattern pattern = Pattern.compile(key + "=.*+\\s");
        Matcher matcher = pattern.matcher(input);
        if(matcher.find()) {
            result = matcher.group();
            result = result.substring(result.indexOf('=') + 1, result.length() - 1);
        }
        return result;
    }
}

Output (add more keys to the key-string if you want get more values):

bluetooth_mac = 45h6kuu
build_type = BFD
tdVersion = 1.2
seloger_mac = F4:0E:83:35:E8:D0
france_mac = F4:0E:83:35:E8:D1

Upvotes: 0

Maurice Perry
Maurice Perry

Reputation: 9651

You could try:

        Pattern re = Pattern.compile("^\\s*(.*)\\s*=(.*)$", Pattern.MULTILINE);
        Matcher matcher = re.matcher(input);
        while (matcher.find()) {
            map.put(matcher.group(1), matcher.group(2));
        }

Upvotes: 1

ashish jhaveri
ashish jhaveri

Reputation: 69

public static void main(String[] args) {
    String str="abc def \n"
            + "key=123 \n "
            + "pass=456 \n"
            + "not working";

        String[] sarray=str.split("\\r?\\n");
        for (String eachline : sarray) {
            System.out.println("line " +  " : " + eachline);
            if(eachline.contains("="))
            {
                String[] sarray2=eachline.split("=");
                System.out.println("key:" +sarray2[0] +":Value:"+ sarray2[1]);
            }
        }
        System.out.println(""+sarray.length);
}

Use split("\r?\n") for new line splitting.

Upvotes: 1

Related Questions