Example usage for java.util.regex Pattern DOTALL

List of usage examples for java.util.regex Pattern DOTALL

Introduction

In this page you can find the example usage for java.util.regex Pattern DOTALL.

Prototype

int DOTALL

To view the source code for java.util.regex Pattern DOTALL.

Click Source Link

Document

Enables dotall mode.

Usage

From source file:Main.java

public static String getRootTag(String xmlStr) {
    Matcher m;//from   w w w.ja  v a2s.co  m
    m = Pattern.compile("^[\r\n \t]*<.*?>[\r\n \t]*<(.*?)>", Pattern.MULTILINE | Pattern.DOTALL)
            .matcher(xmlStr);
    if (m.find()) {
        return m.group(1);
    }
    return "";
}

From source file:Main.java

/**
 * Replaces a tag given by a tagname in an xmlstring.
 * //ww w .  j  av  a2 s.c o m
 * @param tagname Name of the tag to be removed
 * @param xmlstring XmlString to be modified
 * @return modified XmlString
 */
public static String removeTag(String tagname, String xmlstring) {
    if (!patterns.containsKey(tagname)) {
        String regex = "(\\<" + tagname + ">.+?\\</" + tagname + ">)";
        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | Pattern.MULTILINE);
        patterns.put(tagname, pattern);
    }

    Matcher matcher = patterns.get(tagname).matcher(xmlstring);
    if (matcher.find()) {
        xmlstring = matcher.replaceAll("");
    }

    return xmlstring;
}

From source file:Main.java

public static String[] getTagDataArray(String xmlStr, String xmlRoot) {
    List<String> data = new ArrayList<String>();
    Matcher m;/*from w  ww .  j ava2 s  . com*/
    m = Pattern.compile("<" + xmlRoot + ">(.*?)</" + xmlRoot + ">", Pattern.MULTILINE | Pattern.DOTALL)
            .matcher(xmlStr);
    while (m.find()) {
        data.add(xmlToText(m.group(1)));
    }
    return data.toArray(new String[data.size()]);
}

From source file:Main.java

@NonNull
public static List<String> getSplitString(String stringToSplit, int stringSplitLength) {
    /*//from   w w  w.ja va 2  s. c o  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

/**
 * Creates a map of tag name to clean header XML by running a substitution
 * regex on each entry of tag to dirty header XML.
 *
 * @param dirtyXmlMap a map of tag name to dirty header XML
 * @return a map of tag name to clean header XML
 *//*from  ww w  .ja v  a 2 s  . c om*/
private static Map<String, String> createTagToCleanXmlMap(Map<String, String> dirtyXmlMap) {
    Map<String, String> cleanXmlMap = new HashMap<String, String>();
    for (Entry<String, String> sensitiveXml : dirtyXmlMap.entrySet()) {
        Pattern p = Pattern.compile(
                String.format(SENSITIVE_REGEX, sensitiveXml.getKey(), sensitiveXml.getKey()),
                Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        Matcher m = p.matcher(sensitiveXml.getValue());
        if (m.matches()) {
            cleanXmlMap.put(sensitiveXml.getKey(), m.replaceFirst("$1******$2"));
        }
    }
    return cleanXmlMap;
}

From source file:Main.java

/**
 * Parse <xmlRoot name="this.is.name" value="this.is.value"> and return name, value
 *
 * @param xmlRoot xml tag name//from   ww  w .  ja  v  a  2 s .c  o m
 * @param xmlStr string containing xml
 * @return String[]{name, value}
 */
public static String[] getNameValue(String xmlRoot, String xmlStr) {
    String[] strs = new String[] { "", "" };
    Matcher m;
    m = Pattern
            .compile("<" + xmlRoot + " name=\"(.*?)\" value=\"(.*?)\" />", Pattern.MULTILINE | Pattern.DOTALL)
            .matcher(xmlStr);
    if (m.find()) {
        strs[0] = m.group(1);
        strs[1] = m.group(2);
    }
    return strs;
}

From source file:Main.java

/**
 * Pattern.pattern and Pattern.toString ignore any flags supplied to
 * Pattern.compile, so the regular expression you get out doesn't
 * correspond to what the Pattern was actually matching. This fixes that.
 * /*from   ww w.  j a  v  a 2 s  .c  om*/
 * Note that there are some flags that can't be represented.
 * 
 * FIXME: why don't we use Pattern.LITERAL instead of home-grown escaping
 * code? Is it because you can't do the reverse transformation? Should we
 * integrate that code with this?
 */
public static String toString(Pattern pattern) {
    String regex = pattern.pattern();
    final int flags = pattern.flags();
    if (flags != 0) {
        StringBuilder builder = new StringBuilder("(?");
        toStringHelper(builder, flags, Pattern.UNIX_LINES, 'd');
        toStringHelper(builder, flags, Pattern.CASE_INSENSITIVE, 'i');
        toStringHelper(builder, flags, Pattern.COMMENTS, 'x');
        toStringHelper(builder, flags, Pattern.MULTILINE, 'm');
        toStringHelper(builder, flags, Pattern.DOTALL, 's');
        toStringHelper(builder, flags, Pattern.UNICODE_CASE, 'u');
        builder.append(")");
        regex = builder.toString() + regex;
    }
    return regex;
}

From source file:morphy.utils.RegExUtils.java

public static Pattern getPattern(String regularExpression) {
    return Pattern.compile(regularExpression, Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
}

From source file:org.sonarqube.tests.performance.MavenLogs.java

/**
 * Total time: 6.015s//from  w ww  . j  a  va2s.c o m
 * Total time: 3:14.025s
 */
public static Long extractTotalTime(String logs) {
    Pattern pattern = Pattern.compile("^.*Total time: (\\d*:)?(\\d+).(\\d+)s.*$", Pattern.DOTALL);
    Matcher matcher = pattern.matcher(logs);
    if (matcher.matches()) {
        String minutes = StringUtils.defaultIfBlank(StringUtils.removeEnd(matcher.group(1), ":"), "0");
        String seconds = StringUtils.defaultIfBlank(matcher.group(2), "0");
        String millis = StringUtils.defaultIfBlank(matcher.group(3), "0");

        return (Long.parseLong(minutes) * 60000) + (Long.parseLong(seconds) * 1000) + Long.parseLong(millis);
    }
    throw new IllegalStateException("Maven logs do not contain \"Total time\"");
}

From source file:Main.java

/**
 * Remove xml comments from the xml string.
 * /*from w ww .j  a v a  2 s  .c  om*/
 * @param xml
 * @return
 */
public static String stripXMLComment(CharSequence xml) {
    if (xml == null) {
        return null;
    }

    // option DOTALL: '.' matches newlines
    // use '.+?' in stead of '.*': non-greedy matching ("<!-- --> <important-stuff\> <!-- -->" is replaced with " <important-stuff\> ")
    Pattern p = Pattern.compile("<!--.+?-->", Pattern.DOTALL); //$NON-NLS-1$
    Matcher m = p.matcher(xml);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, ""); //$NON-NLS-1$
    }
    m.appendTail(sb);
    return sb.toString();
}