Skip to content

BelmoMusta/regex-scanner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

regex-scanner

Codacy Badge

This repository contains some utilities to extract tokens that match a given regex from a string.

For example, when you want to extract lines that represent a key:value items :

public static Map<String, String> getMapFromKeyValuePairs(String keyValuePairs) {
        final String regex = "\\w+[\\t ]*:[\\t ]*\\w+";
        final RegexScanner regexScanner = new RegexScanner(keyValuePairs, regex);
        final Map<String, String> keyValueMap = new LinkedHashMap<>();

        while (regexScanner.hasNext()) {
            final Function<String, String[]> mapper = token -> token.split("[\\t ]*:[\\t ]*");
            String[] split = regexScanner.next(mapper);
            keyValueMap.put(split[0], split[1]);
        }
        return keyValueMap;
    }

    public static void main(String[] args) {
            final String keyValuePairs = "key0:0\n" +
                    "key1:   1\n" +
                    "key2: 10\n" +
                    "key3  :11\n" +
                    "key4:  111";

            Map<String, String> mapFromKeyValuePairs = getMapFromKeyValuePairs(keyValuePairs);
            System.out.println(mapFromKeyValuePairs);
        }

Output

{key0=0, key1=1, key2=10, key3=11, key4=111}

Notice that you may use the RegexScanner.next() if you do not want to convert the found tokens.

In the previous example a mapper is used to convert the found token to an array of paired key values.

About

Regex based java scanner

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages