Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    public static void main(final String[] args) {
        final String[] lines = new String[] { "1. New", "2. UK", "3. France" };

        final Pattern p = Pattern.compile("\\.\\s+(.*?)\\s+-\\s+(.*)");

        for (final String unparsedText : lines) {
            String newspaper;
            String country;

            final Matcher m = p.matcher(unparsedText);

            if (m.find()) {
                newspaper = m.group(1);
                country = m.group(2);

                System.out.println("Newspaper: " + newspaper + " Country: " + country);
            }
        }
    }
}