get First Number by Regex - Java java.util.regex

Java examples for java.util.regex:Match Number

Description

get First Number by Regex

Demo Code


//package com.java2s;

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

public class Main {

    public static int getFirstNumber(String StringWithNumber) {
        return Integer.parseInt(get("\\d+", StringWithNumber, 0));
    }/*w ww. ja v a2 s  .  co m*/

    public static String get(String regex, String content, int groupIndex) {
        Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE)
                .matcher(content);
        if (matcher.find()) {
            return matcher.group(groupIndex);
        }
        return null;
    }
}

Related Tutorials