Here you can find the source of regexExtract(String string, String pattern)
Parameter | Description |
---|---|
string | to search |
pattern | regex pattern |
public static String regexExtract(String string, String pattern)
//package com.java2s; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /**//www. ja va 2 s. c o m * Extracts regex match from string * @param string to search * @param pattern regex pattern * @return matched string, or null if nothing found (or null if any of the parameters is null) */ public static String regexExtract(String string, String pattern) { if (string == null || pattern == null) { return null; } Matcher m = regexGetMatcherForPattern(string, pattern); if (m == null) { return null; } if (m.find()) { return m.group(0); } else return null; } public static Matcher regexGetMatcherForPattern(String string, String pattern) { if (string == null || pattern == null) { return null; } /** * strip newline characters because it doesn't work well with this. */ // string = string.replace("\n", ""); Pattern p = Pattern.compile(pattern, Pattern.DOTALL | Pattern.MULTILINE); Matcher m = p.matcher(string); return m; } }