Example usage for java.util.regex Matcher group

List of usage examples for java.util.regex Matcher group

Introduction

In this page you can find the example usage for java.util.regex Matcher group.

Prototype

public String group(String name) 

Source Link

Document

Returns the input subsequence captured by the given named-capturing group during the previous match operation.

Usage

From source file:Main.java

/**
 * Extracts the objid from the provided resource XML representation.<br/> Either the first occurence of objid="..."
 * is searched and the value is returned, or the first occurence of :href="..." is searched and from this value the
 * objid is extracted and returned.//from   w w w  . j  a v  a 2s  .  c o m
 *
 * @param resourceXml The XML representation of the resource to get the objid from.
 * @return Returns the extracted objid or {@code null}.
 */
public static String getIdFromXml(final CharSequence resourceXml) {

    final Matcher matcher = PATTERN_OBJID_FROM_XML.matcher(resourceXml);
    return matcher.find() ? matcher.group(2) : null;
}

From source file:Main.java

public static String getYoutubeVideoId(String in) {

    //*EDIT* - fixed to hopefully support more recent youtube link styles/formats:
    final String pattern = "(?<=watch\\?v=|/videos/|/embed/|youtu.be/)[^&#?]*";
    final String pattern2 = "(?:youtube(?:-nocookie)?\\.com\\/(?:[^\\/\\n\\s]+\\/\\S+\\/|(?:v|e(?:mbed)?)\\/|\\S*?[?&]v=)|youtu\\.be\\/)([a-zA-Z0-9_-]{11})";

    Pattern compiledPattern = Pattern.compile(pattern2, Pattern.CASE_INSENSITIVE);
    Matcher matcher = compiledPattern.matcher(in);

    if (matcher.find()) {
        return matcher.group(1);
    }//  w w w  .  j ava2s  .  c  o  m

    return null;
}

From source file:Main.java

/**
 * Given a path of the form <code>{doc|document}('<i>aDoc.xml</i>')<i>/aPath</i></code>, it divides it into the doc and the path
 * @param pathWithDoc a path starting with the XPath 'doc' function or the XSLT 'document' function.
 * @return a string array such that array[0]=<i>docPath</i>, array[1]=<i>path</i> and array[2]=<i>pathWithoutRootElement</i> 
 *//*from   w w w  . jav a 2  s  . c o m*/
public static String[] divideDocAndPath(String pathWithDoc) {
    String[] result = new String[3];
    final String REGEXP_SEARCH = "^(doc|document)\\('(?<docName>.+)'\\)(?<path>/[^\\[\\]/]+(\\[.+\\])?(?<pathWithoutRoot>/.+))$";
    Pattern patternToSearch = Pattern.compile(REGEXP_SEARCH);
    Matcher matcher = patternToSearch.matcher(pathWithDoc);
    matcher.matches();
    result[0] = matcher.group("docName");
    result[1] = matcher.group("path");
    result[2] = matcher.group("pathWithoutRoot");
    return result;
}

From source file:Main.java

@NonNull
public static List<String> getSplitString(String stringToSplit, int stringSplitLength) {
    /*/*from ww w  .j av  a2  s.co  m*/
       length to split string needs to be at least as long as the longest series of non-whitespace in the string.
       E.G. if the following is the longest series of non-whitespace in the string
      "licences:"
       then stringSplitLength needs to be at least 9 (8 letters plus a colon)
     */
    stringSplitLength = Math.max(getLengthOfLongestWord(stringToSplit), stringSplitLength);

    List<String> splitString = new ArrayList<>();

    Pattern p = Pattern.compile("\\G\\s*(.{1," + stringSplitLength + "})(?=\\s|$)", Pattern.DOTALL);
    Matcher m2 = p.matcher(stringToSplit);
    while (m2.find()) {
        splitString.add(m2.group(1));
    }
    return splitString;
}

From source file:Main.java

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);
    }// w w  w.j  a v  a  2 s.co m
    return null;
}

From source file:gov.nih.nci.caarray.web.util.UserComparator.java

/**
 * Extracts last and first name from a string like <code>&lt;a href="..."&gt;last, first&lt;/a&gt;</code>.
 * @param s the html to extrat names from
 * @return UPPER(last), UPPER(first)/*  w w w .ja  v  a2 s .co  m*/
 */
public static String[] getNames(String s) {
    Pattern p = Pattern.compile("[^>]*>\\s*(.*),\\s*(.*)<[^<]*");
    Matcher m = p.matcher(s.toUpperCase(Locale.US));
    m.lookingAt();
    return new String[] { m.group(1).trim(), m.group(2).trim() };
}

From source file:Main.java

public static float getSize(String size) {
    float value = 15.0f;
    if (size != null) {
        Matcher m = SIZED_VALUE.matcher(size.trim());
        if (m.matches()) {
            value = Float.parseFloat(m.group(1));
        }//w w w  .  jav  a  2s. c  o  m
    }

    return value;
}

From source file:Main.java

/**
 * Extract version number from objid.//from  w  w w.  j  av a2s . c  o  m
 *
 * @param objid The objid.
 * @return The number of version or null.
 */
public static String getVersionNumberFromObjid(final CharSequence objid) {
    String version = null;
    final Matcher m = PATTERN_VERSION_NUMBER.matcher(objid);
    if (m.find()) {
        version = m.group(1);
    }
    return version;
}

From source file:Main.java

public static String getNumber(String input) {
    if (input == null)
        return "";
    String str = input.replaceAll("\\s+", "");
    Pattern number = Pattern.compile("[0-9]+");
    Matcher matcher1 = number.matcher(str);
    if (matcher1.find())
        return matcher1.group(0);
    return "";
}

From source file:Main.java

public static long parseDuration(String icalDuration) {
    final int GROUP_SECONDS = 1;
    final String PATTERN = "-?P(\\d)+S";

    long durationInSeconds = -1;

    Pattern p = Pattern.compile(PATTERN);
    Matcher m = p.matcher(icalDuration);
    if (m.groupCount() > 1) {
        durationInSeconds = Long.parseLong(m.group(GROUP_SECONDS));
    }//from www  . j  a  v  a  2s .co  m

    return durationInSeconds;
}