简体   繁体   中英

Is there any Java based open source framework to find values from a text field based on key string with delimiter?

Is there any Java based open source framework to find values in a text field based on key string with delimiter?

Example:

Key: username Start-Delimiter: ; End-Delimiter: ;

Key: on Start-Delimiter: ; End-Delimiter: ;

Sample Input: A user with username ;surenraju; logged into the system on ;Thu May 2, 2013 2:30pm;

Results: username - surenraju
on - Thu May 2, 2013 2:30pm

Thanks in advance.

We have solved this problem with Regex.

Sample code:

    ArrayList<String> keys = new ArrayList<String>();
    keys.add("username");
    keys.add("on");
    String startDelimiter = ":";
    String endDelimiter = ":";
    String searchStr = "A user with username :suren: logged into the system on :22 May 2013 2:30 PM:";
    for (String key : keys) {
        String pattern = "("+key+")[ ]*?"+startDelimiter+"([^" +endDelimiter+ "]+)"+endDelimiter;
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(searchStr);
        while (m.find()) {
            System.out.println("Key: " + m.group(1) + " Value: "
                    + m.group(2));
        }
    }

Tests

I. Key: username Start-Delimiter: ; End-Delimiter: ;

Need to find a value which if followed by key( in this case username ) and value is between start and end delimiters(in this case ;).

II. Key: on Start-Delimiter: ; End-Delimiter: ;

Sample Input:

A user with username ;suren; logged into the system on ;Thu May 2, 2013 2:30pm;

Results: I. username - suren II. on - Thu May 2, 2013 2:30pm

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