DEV Community

Cover image for Java REGEX based scanner
Mustapha Belmokhtar
Mustapha Belmokhtar

Posted on

2 2

Java REGEX based scanner

I often use the java built-in java.util.Scanner in order to retrieve information from string streams, but what I wasn't able to do, is to extract some tokens that match a predefined regex, and then consume the character sequence till the end.
For the java built-in scanner example, the regex is used only to define the delimiters, for example the tokens are separated by blank spaces, say , \t, or \n.
I could not tell it to retrieve well defined information, for example 123a4567-e89b-12d3-a456-123442445670, especially when it is inside a text mixed with some other tokens.
All I have to do now is to provide the regex example, and I get all the matching tokens from the given text.

Exemple

@Test
    public void testUUID() {
        final String regex = "[0-9abcdef]{8}(-[0-9abcdef]{4}){3}-[0-9abcdef]{12}";
        final String text = "uuid : 6d0a3538-9760-41ae-965d-7aad70021f81\n" +
                "uuid : d7d97fb3-3676-4109-9a94-7acc5f593ace\n" +
                "uuid : 02e87dd3-10ff-43cf-9572-bd9d151bb439\n" +
                "uuid : 632a4c31-8dfe-43a3-8f8d-15b472292cc9";

        final List<String> expectedUUIDs = Arrays.asList("6d0a3538-9760-41ae-965d-7aad70021f81",
                "d7d97fb3-3676-4109-9a94-7acc5f593ace",
                "02e87dd3-10ff-43cf-9572-bd9d151bb439",
                "632a4c31-8dfe-43a3-8f8d-15b472292cc9");

        final List<String> foundUUIDs = new ArrayList<>();
        final RegexScanner regexScanner = new RegexScanner(text, regex);
        while (regexScanner.hasNext()) {
            foundUUIDs.add(regexScanner.next());
        }
        Assert.assertArrayEquals(expectedUUIDs.toArray(), foundUUIDs.toArray());
    }

This way, the scanner is consumed by the while loop until the end is reached, which means all the token are read and processed.

It is possible also to provide a function that maps the found token to another object using the next(Function<String ,R> mapper) method.

The code is available on github via this link.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay