简体   繁体   中英

Regex to find a word if it presents java

I have a file with structured data but it may have a missing field as follows

Name:xxxxx,Age:20,Gender:Male
Name:yyyy,Gender:male
Name:zzzx,Age:26,Gender:Male

I want a single regex to get the values of name,age and gender. So far I have used

Name:(.*),Age:(.*),Gender:(.*)

which brings the values if all three fields exists but it doesnt works for the missing fileds. can anyone tell me how to achieve this

Are you using Java's Pattern and Matcher classes? If so, you can do a regular expression like so.

(Name:(.*),)?(Age:(.*),)?(Gender:(.*),)?

This will mess up your grouping some but should give the correct results. The reason this works is because when you use a '?' it finds zero or one instances of what it's looks for. So this is perfect for what you are doing.

Split on , and : to get all key-value pairs.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SOPlayground {

    public static void main(String[] args) throws Exception {
        String input = "Name:xxxxx,Age:20,Gender:Male\n"
                + "Name:yyyy,Gender:male\n"
                + "Name:zzzx,Age:26,Gender:Male";

        List<Map<String, String>> entries = new ArrayList<>();
        for (String line : input.split("\n")) {
            Map<String, String> entry = new HashMap<>();
            for (String field : line.split(",")) {
                String[] parts = field.split(":");
                String key = parts[0];
                String value = parts[1];
                entry.put(key, value);
            }
            entries.add(entry);
        }
        System.out.println(entries);
    }

}

Output:

[{Gender=Male, Age=20, Name=xxxxx}, {Gender=male, Name=yyyy}, {Gender=Male, Age=26, Name=zzzx}]

Try out something like:

String str = "Name:xxxxx,Age:20,Gender:Male";
String regex = "Name:(.*?[^,]),Age:(.*?[^,]),Gender:(.*)"; 
...
Output:
xxxxx
20
Male

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM