Example usage for java.util.regex Pattern CASE_INSENSITIVE

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

Introduction

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

Prototype

int CASE_INSENSITIVE

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

Click Source Link

Document

Enables case-insensitive matching.

Usage

From source file:uk.ac.kcl.it.DeIdentificationPKPartitionWithoutScheduling.java

@Test
public void deidentificationPerformanceTest() {
    dbmsTestUtils.createBasicInputTable();
    dbmsTestUtils.createBasicOutputTable();
    dbmsTestUtils.createDeIdInputTable();
    List<Mutant> mutants = testUtils.insertTestDataForDeidentification(env.getProperty("tblIdentifiers"),
            env.getProperty("tblInputDocs"), mutatortype);
    int totalTruePositives = 0;
    int totalFalsePositives = 0;
    int totalFalseNegatives = 0;

    for (Mutant mutant : mutants) {
        Set<Pattern> mutatedPatterns = new HashSet<>();
        mutant.setDeidentifiedString(elasticGazetteerService.deIdentifyString(mutant.getFinalText(),
                String.valueOf(mutant.getDocumentid())));
        Set<String> set = new HashSet<>(mutant.getOutputTokens());
        mutatedPatterns.addAll(//w w w  . j a v a2 s.  co  m
                set.stream().map(string -> Pattern.compile(Pattern.quote(string), Pattern.CASE_INSENSITIVE))
                        .collect(Collectors.toSet()));
        List<MatchResult> results = new ArrayList<>();
        for (Pattern pattern : mutatedPatterns) {
            Matcher matcher = pattern.matcher(mutant.getFinalText());
            while (matcher.find()) {
                results.add(matcher.toMatchResult());
            }
        }

        int truePositives = getTruePositiveTokenCount(mutant);
        int falsePositives = getFalsePositiveTokenCount(mutant);
        int falseNegatives = getFalseNegativeTokenCount(mutant);

        System.out.println("Doc ID " + mutant.getDocumentid() + " has " + falseNegatives
                + " unmasked identifiers from a total of " + (falseNegatives + truePositives));
        System.out.println("Doc ID " + mutant.getDocumentid() + " has " + falsePositives
                + " inaccurately masked tokens from a total of " + (falsePositives + truePositives));
        System.out.println("TP: " + truePositives + " FP: " + falsePositives + " FN: " + falseNegatives);
        System.out.println("Doc ID precision " + calcPrecision(falsePositives, truePositives));
        System.out.println("Doc ID recall " + calcRecall(falseNegatives, truePositives));
        System.out.println(mutant.getDeidentifiedString());
        System.out.println(mutant.getFinalText());
        System.out.println(mutant.getInputTokens());
        System.out.println(mutant.getOutputTokens());
        System.out.println();
        if (env.getProperty("elasticgazetteerTestOutput") != null) {
            try {
                try (BufferedWriter bw = new BufferedWriter(
                        new FileWriter(new File(env.getProperty("elasticgazetteerTestOutput") + File.separator
                                + mutant.getDocumentid())))) {
                    bw.write("Doc ID " + mutant.getDocumentid() + " has " + falseNegatives
                            + " unmasked identifiers from a total of " + (falseNegatives + truePositives));
                    bw.newLine();
                    bw.write("Doc ID " + mutant.getDocumentid() + " has " + falsePositives
                            + " inaccurately masked tokens from a total of "
                            + (falsePositives + truePositives));
                    bw.newLine();
                    bw.write("TP: " + truePositives + " FP: " + falsePositives + " FN: " + falseNegatives);
                    bw.newLine();
                    bw.write("Doc ID precision " + calcPrecision(falsePositives, truePositives));
                    bw.newLine();
                    bw.write("Doc ID recall " + calcRecall(falseNegatives, truePositives));
                    bw.newLine();
                    bw.write(mutant.getDeidentifiedString());
                    bw.newLine();
                    bw.write(mutant.getFinalText());
                    bw.newLine();
                    bw.write(mutant.getInputTokens().toString());
                    bw.newLine();
                    bw.write(mutant.getOutputTokens().toString());

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        totalTruePositives += truePositives;
        totalFalsePositives += falsePositives;
        totalFalseNegatives += falseNegatives;
    }
    System.out.println();
    System.out.println();
    System.out.println("THIS RUN TP: " + totalTruePositives + " FP: " + totalFalsePositives + " FN: "
            + totalFalseNegatives);
    System.out.println("Doc ID precision " + calcPrecision(totalFalsePositives, totalTruePositives));
    System.out.println("Doc ID recall " + calcRecall(totalFalseNegatives, totalTruePositives));
    if (env.getProperty("elasticgazetteerTestOutput") != null) {
        try {
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(
                    new File(env.getProperty("elasticgazetteerTestOutput") + File.separator + "summary")))) {
                bw.write("THIS RUN TP: " + totalTruePositives + " FP: " + totalFalsePositives + " FN: "
                        + totalFalseNegatives);
                bw.newLine();
                bw.write("Doc ID precision " + calcPrecision(totalFalsePositives, totalTruePositives));
                bw.newLine();
                bw.write("Doc ID recall " + calcRecall(totalFalseNegatives, totalTruePositives));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:de.unirostock.sems.cbarchive.web.HttpImporter.java

private File downloadFile() throws ImporterException {

    try {//from   w  w  w.j av a2  s. c  o  m
        tempFile = File.createTempFile(Fields.TEMP_FILE_PREFIX, ".omex");
        HttpResponse getResponse = client.execute(new HttpGet(remoteUrl));

        // check if file exists
        if (getResponse.getStatusLine().getStatusCode() != 200) {
            LOGGER.warn(getResponse.getStatusLine().getStatusCode(), " ",
                    getResponse.getStatusLine().getReasonPhrase(), " while download ", remoteUrl);
            throw new ImporterException(String.valueOf(getResponse.getStatusLine().getStatusCode()) + " "
                    + getResponse.getStatusLine().getReasonPhrase() + " while download");
        }

        HttpEntity entity = getResponse.getEntity();
        if (entity == null) {
            LOGGER.error("No content returned while donwloading remote file ", remoteUrl);
            throw new ImporterException("No content returned while donwloading remote file " + remoteUrl);
        }

        // for name suggestions
        Header dispositionHeader = getResponse.getFirstHeader("Content-Disposition");
        if (dispositionHeader != null && dispositionHeader.getValue() != null
                && dispositionHeader.getValue().isEmpty() == false) {
            // disposition header is present -> extract name
            // inline; filename=\"{0}.{1}\"
            Matcher matcher = Pattern
                    .compile("filename=\\\"?(([a-zA-Z0-9-_\\+]+).(\\w+))\\\"?", Pattern.CASE_INSENSITIVE)
                    .matcher(dispositionHeader.getValue());
            if (matcher.find()) {
                suggestedName = matcher.group(1);
                for (int i = 0; i < matcher.groupCount(); i++)
                    LOGGER.debug(i, ": ", matcher.group(i));
            }
        } else {
            // when not -> take the last part of the url
            Matcher matcher = Pattern.compile("\\/(([a-zA-Z0-9-_\\+]+).(\\w+))$", Pattern.CASE_INSENSITIVE)
                    .matcher(remoteUrl);
            if (matcher.find()) {
                suggestedName = matcher.group(1);
                for (int i = 0; i < matcher.groupCount(); i++)
                    LOGGER.debug(i, ": ", matcher.group(i));
            }

        }

        // download it
        OutputStream output = new FileOutputStream(tempFile);
        IOUtils.copy(entity.getContent(), output);

        // check against quota
        if (length != tempFile.length()) {
            LOGGER.warn("Content-Length (", length, ") and downloaded length (", tempFile.length(),
                    ") are different.");
            length = tempFile.length();

            checkQuotas();
        }

        return tempFile;

    } catch (IOException e) {
        LOGGER.error(e, "Exception while download file from ", remoteUrl);
        throw new ImporterException("Exception while download remote file", e);
    }
}

From source file:com.redhat.rhn.domain.channel.NewChannelHelper.java

/**
 * Verifies a potential name for a channel
 * @param name the name of the channel/* w  ww.j a  va  2 s  . com*/
 * @return true if it is correct, false otherwise
 */
public static boolean verifyName(String name) {

    if (name.length() < 6) {
        return false;
    }

    Pattern pattern = Pattern.compile("^(rhn|red\\s*hat).*", Pattern.CASE_INSENSITIVE);
    Matcher match = pattern.matcher(name);
    if (match.matches()) {
        return false;
    }
    pattern = Pattern.compile("^[a-z][\\w\\d\\s\\-\\.\\'\\(\\)\\/\\_]*$", Pattern.CASE_INSENSITIVE);
    match = pattern.matcher(name);
    if (!match.matches()) {
        return false;
    }
    return true;

}

From source file:com.mycompany.myproject.xemailservice.tests.XEmailIntegrationIT.java

@Test
public void testGetFormFields() {
    RequestExecutor exec = null;//from w  w  w .  j  av a  2  s .c o  m
    try {
        String action = "mcm/components/emailserviceactions/actions/addSubscriber";
        String path = "/_jcr_content.emailservice.json?operation=getFormFields&cfgpath=" + XConfigPath
                + "&actionType=" + action;
        exec = authorAdmin.http.doGet(path, SC_OK);
        String content = exec.getContent();
        assertNotNull(content);

        final Pattern p = Pattern.compile("\"type\":\"email\"", Pattern.CASE_INSENSITIVE);
        if (!p.matcher(content).find())
            fail("Couldnot retreive Attribute--Email");

    } catch (Exception e) {
        Assert.fail("Could not retreive default attribute email from XMailService" + e.getMessage());
    }
}

From source file:wuit.common.crawler.WebSit.Crawler.java

public static void matchValues(String content, String filter, List<KeyValue> list) {
    if (list == null)
        list = new ArrayList<KeyValue>();
    try {//from w  w  w.  jav  a2  s . c  o  m
        Matcher m = Pattern.compile(filter, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE).matcher(content);
        while (m.find()) {
            if (m.group().isEmpty())
                continue;
            KeyValue value = new KeyValue();
            value.value = m.group();
            value.start = m.start();
            value.end = m.end();
            list.add(value);
        }
    } catch (Exception e) {
        System.out.println("Crawler Utitles  matchValues :" + e.getMessage());
    }
}

From source file:gsn.storage.SQLUtils.java

public static StringBuilder newRewrite(CharSequence query, CharSequence tableNameToRename,
        CharSequence replaceTo) {
    // Selecting strings between pair of "" : (\"[^\"]*\")
    // Selecting tableID.tableName or tableID.* : (\\w+(\\.(\w+)|\\*))
    // The combined pattern is : (\"[^\"]*\")|(\\w+\\.((\\w+)|\\*))
    Matcher matcher = pattern.matcher(query);
    StringBuffer result = new StringBuffer();
    while (matcher.find()) {
        if (matcher.group(2) == null)
            continue;
        String tableName = matcher.group(3);
        if (tableName.equals(tableNameToRename)) {
            // $4 means that the 4th group of the match should be appended to the
            // string (the forth group contains the field name).
            if (replaceTo != null)
                matcher.appendReplacement(result, new StringBuilder(replaceTo).append("$4").toString());
        }/*from w  w w  .ja v a2s . co  m*/
    }
    String toReturn = matcher.appendTail(result).toString().toLowerCase();
    int indexOfFrom = toReturn.indexOf(" from ") >= 0 ? toReturn.indexOf(" from ") + " from ".length() : 0;
    int indexOfWhere = (toReturn.lastIndexOf(" where ") > 0 ? (toReturn.lastIndexOf(" where "))
            : toReturn.length());
    String selection = toReturn.substring(indexOfFrom, indexOfWhere);
    Pattern fromClausePattern = Pattern.compile("\\s*(\\w+)\\s*", Pattern.CASE_INSENSITIVE);
    Matcher fromClauseMather = fromClausePattern.matcher(selection);
    result = new StringBuffer();
    while (fromClauseMather.find()) {
        if (fromClauseMather.group(1) == null)
            continue;
        String tableName = fromClauseMather.group(1);
        if (tableName.equals(tableNameToRename) && replaceTo != null)
            fromClauseMather.appendReplacement(result, replaceTo.toString() + " ");
    }
    String cleanFromClause = fromClauseMather.appendTail(result).toString();
    String finalResult = StringUtils.replace(toReturn, selection, cleanFromClause);
    return new StringBuilder(finalResult);
}

From source file:csiro.pidsvc.core.Settings.java

public boolean isNewVersionAvailable() {
    if (_manifest == null)
        return false;

    String content = Http.simpleGetRequest(getProperty("buildRepository"));
    Pattern re = Pattern.compile("href=\"pidsvc-(\\d+\\.\\d+)(?:-SNAPSHOT)?\\.(.+?)\\.war\"",
            Pattern.CASE_INSENSITIVE);
    Matcher m = re.matcher(content);

    try {/*from   w ww  .  j av a  2 s  .  co  m*/
        if (m.find()) {
            String currentVersion = _manifest.getMainAttributes().getValue("Implementation-Build");
            String newVersion = m.group(2);

            if (!currentVersion.isEmpty() && !newVersion.equalsIgnoreCase(currentVersion))
                return true;
        }
    } catch (Exception e) {
        _logger.debug(e);
    }
    return false;
}

From source file:org.shredzone.cilla.service.impl.PageServiceImpl.java

@Override
public boolean isAcceptedResponse(Page page, String response) {
    if (!page.isRestricted()) {
        // If the page is not restricted, the response is always acceptable
        return true;
    }//from   w  w  w  . j a v a2  s .  c om

    Pattern pattern = Pattern.compile(page.getResponsePattern(), Pattern.CASE_INSENSITIVE);
    return (pattern.matcher(response).matches());
}

From source file:com.hangum.tadpole.engine.restful.RESTfulAPIUtils.java

/**
 * user validate/*  w  w  w . jav a  2s .co m*/
 * 
 * @param url
 * @return
 */
public static boolean validateURL(String url) {
    Pattern p = Pattern.compile("[/][-A-Za-z0-9+&amp;@#/%=~_()|]{2}", Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(url);

    return m.find();
}

From source file:net.duckling.ddl.service.resource.impl.FolderPathServiceImpl.java

private String getResourceName(List<Resource> rs, String fileName, String itemType) {
    if (rs == null || rs.isEmpty()) {
        return fileName;
    }/*from  www .  jav  a  2 s.c  o  m*/
    int max = 0;
    String reg = getPattenName(fileName, itemType, "\\((\\d+)\\)");
    try {
        Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
        for (Resource file : rs) {
            Matcher ma = pattern.matcher(file.getTitle());
            if (ma.matches()) {
                String id = ma.group(1);
                try {
                    int tmp = Integer.parseInt(id);
                    if (tmp > max) {
                        max = tmp;
                    }
                } catch (RuntimeException e) {

                }
            }
        }

    } catch (RuntimeException e) {
        LOG.error("", e);
        return fileName;
    }
    max++;
    return getQueryName(fileName, itemType, "(" + max + ")");
}