Example usage for java.util.regex Matcher replaceFirst

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

Introduction

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

Prototype

public String replaceFirst(Function<MatchResult, String> replacer) 

Source Link

Document

Replaces the first subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence.

Usage

From source file:org.loklak.api.search.TweetScraper.java

public static String unshorten(String text) {
    while (true) {
        try {/* w  ww  .j  a v  a2  s  . c o m*/
            Matcher m = emoji_pattern.matcher(text);
            if (m.find()) {
                String emoji = m.group(1);
                text = m.replaceFirst(emoji);
                continue;
            }
        } catch (Throwable e) {
            DAO.severe(e);
            break;
        }
        try {
            Matcher m = emoji_pattern_span.matcher(text);
            if (m.find()) {
                String emoji = m.group(1);
                text = m.replaceFirst(emoji);
                continue;
            }
        } catch (Throwable e) {
            DAO.severe(e);
            break;
        }
        try {
            Matcher m = hashtag_pattern.matcher(text);
            if (m.find()) {
                text = m.replaceFirst(" #" + m.group(1) + " "); // the extra spaces are needed because twitter removes them if the hashtag is followed with a link
                continue;
            }
        } catch (Throwable e) {
            DAO.severe(e);
            break;
        }
        try {
            Matcher m = timeline_link_pattern.matcher(text);
            if (m.find()) {
                String expanded = RedirectUnshortener.unShorten(m.group(2));
                text = m.replaceFirst(" " + expanded);
                continue;
            }
        } catch (Throwable e) {
            DAO.severe(e);
            break;
        }
        try {
            Matcher m = timeline_embed_pattern.matcher(text);
            if (m.find()) {
                text = m.replaceFirst("");
                continue;
            }
        } catch (Throwable e) {
            DAO.severe(e);
            break;
        }
    }
    text = cleanup_pattern.matcher(text).replaceAll("");
    text = MessageEntry.html2utf8(text);
    text = doublespace_pattern.matcher(text).replaceAll(" ");
    text = text.trim();
    return text;
}

From source file:org.tinymediamanager.core.movie.connector.MovieToXbmcNfoConnector.java

protected static MovieToXbmcNfoConnector parseNFO(Path nfoFile) throws Exception {
    Unmarshaller um = context.createUnmarshaller();
    if (um == null) {
        MessageManager.instance.pushMessage(new Message(MessageLevel.ERROR, nfoFile, "message.nfo.readerror"));
        throw new Exception("could not create unmarshaller");
    }//from  w w  w  .j a va  2  s .c  om

    MovieToXbmcNfoConnector xbmc = null;
    Reader in = null;
    try {
        in = new InputStreamReader(new FileInputStream(nfoFile.toFile()), "UTF-8");
        xbmc = (MovieToXbmcNfoConnector) um.unmarshal(in);
    } catch (UnmarshalException | IllegalArgumentException e) {
    } finally {
        if (in != null) {
            in.close();
        }
    }

    if (xbmc == null) {
        // now trying to parse it via string
        String completeNFO = Utils.readFileToString(nfoFile).trim().replaceFirst("^([\\W]+)<", "<");
        Matcher matcher = PATTERN_NFO_MOVIE_TAG.matcher(completeNFO);
        if (matcher.find()) {
            completeNFO = matcher.replaceFirst(
                    "<movie xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
        }
        try {
            in = new StringReader(ParserUtils.cleanNfo(completeNFO));
            xbmc = (MovieToXbmcNfoConnector) um.unmarshal(in);
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
    return xbmc;
}

From source file:org.tinymediamanager.core.movie.connector.MovieToKodiNfoConnector.java

protected static MovieToKodiNfoConnector parseNFO(Path nfoFile) throws Exception {
    Unmarshaller um = context.createUnmarshaller();
    if (um == null) {
        MessageManager.instance.pushMessage(new Message(MessageLevel.ERROR, nfoFile, "message.nfo.readerror"));
        throw new Exception("could not create unmarshaller");
    }//from   w  w  w  .java2 s  .c  o  m

    MovieToKodiNfoConnector kodi = null;
    Reader in = null;
    try {
        in = new InputStreamReader(new FileInputStream(nfoFile.toFile()), "UTF-8");
        kodi = (MovieToKodiNfoConnector) um.unmarshal(in);
    } catch (UnmarshalException | IllegalArgumentException e) {
    } finally {
        if (in != null) {
            in.close();
        }
    }

    if (kodi == null) {
        // now trying to parse it via string
        String completeNFO = Utils.readFileToString(nfoFile).trim().replaceFirst("^([\\W]+)<", "<");
        Matcher matcher = PATTERN_NFO_MOVIE_TAG.matcher(completeNFO);
        if (matcher.find()) {
            completeNFO = matcher.replaceFirst(
                    "<movie xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
        }
        try {
            in = new StringReader(ParserUtils.cleanNfo(completeNFO));
            kodi = (MovieToKodiNfoConnector) um.unmarshal(in);
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
    return kodi;
}

From source file:org.deeplearning4j.nn.modelimport.keras.KerasModelImport.java

/**
 * Read Keras model weights from specified HDF5 file and Group into a map
 * from layer to parameter to weights (INDArray).
 *
 * @param file                   open HDF5 archive file
 * @param weightsGroupName       name of root HDF5 Group storing all Keras weights for single model
 * @return              nested Map from layer names to parameter names to INDArrays
 *//*w  w  w  .  j  a va  2  s  .c om*/
private static Map<String, Map<String, INDArray>> readWeightsFromHdf5(hdf5.H5File file, String weightsGroupName)
        throws UnsupportedKerasConfigurationException {
    hdf5.Group weightsGroup = file.asCommonFG().openGroup(weightsGroupName);

    Map<String, Map<String, INDArray>> weightsMap = new HashMap<String, Map<String, INDArray>>();

    List<hdf5.Group> groups = new ArrayList<hdf5.Group>();
    groups.add(weightsGroup);
    while (!groups.isEmpty()) {
        hdf5.Group g = groups.remove(0);
        for (int i = 0; i < g.asCommonFG().getNumObjs(); i++) {
            BytePointer objPtr = g.asCommonFG().getObjnameByIdx(i);
            String objName = objPtr.getString();
            int objType = g.asCommonFG().childObjType(objPtr);
            switch (objType) {
            case H5O_TYPE_DATASET:
                /* Keras parameter names are typically formatted as [layer name]_[layer no]_[parameter].
                 * For example, the weight matrix in the first Dense layer will be named "dense_1_W."
                 */
                String[] tokens = objName.split("_");
                String layerName = StringUtils.join(Arrays.copyOfRange(tokens, 0, 2), "_");
                String paramName = StringUtils.join(Arrays.copyOfRange(tokens, 2, tokens.length), "_");
                /* TensorFlow backend often appends ":" followed by one
                 * or more digits to parameter names, but this is not
                 * reflected in the model config. We must strip it off.
                 */
                Pattern p = Pattern.compile(":\\d+$");
                Matcher m = p.matcher(paramName);

                hdf5.DataSet d = g.asCommonFG().openDataSet(objPtr);
                hdf5.DataSpace space = d.getSpace();
                int nbDims = (int) space.getSimpleExtentNdims();
                long[] dims = new long[nbDims];
                space.getSimpleExtentDims(dims);
                float[] weightBuffer = null;
                FloatPointer fp = null;
                int j = 0;
                INDArray weights = null;
                if (m.find())
                    paramName = m.replaceFirst("");
                switch (nbDims) {
                case 4: /* 2D Convolution weights */
                    weightBuffer = new float[(int) (dims[0] * dims[1] * dims[2] * dims[3])];
                    fp = new FloatPointer(weightBuffer);
                    d.read(fp, new hdf5.DataType(hdf5.PredType.NATIVE_FLOAT()));
                    fp.get(weightBuffer);
                    weights = Nd4j.create((int) dims[0], (int) dims[1], (int) dims[2], (int) dims[3]);
                    j = 0;
                    for (int i1 = 0; i1 < dims[0]; i1++)
                        for (int i2 = 0; i2 < dims[1]; i2++)
                            for (int i3 = 0; i3 < dims[2]; i3++)
                                for (int i4 = 0; i4 < dims[3]; i4++)
                                    weights.putScalar(i1, i2, i3, i4, weightBuffer[j++]);
                    break;
                case 2: /* Dense and Recurrent weights */
                    weightBuffer = new float[(int) (dims[0] * dims[1])];
                    fp = new FloatPointer(weightBuffer);
                    d.read(fp, new hdf5.DataType(hdf5.PredType.NATIVE_FLOAT()));
                    fp.get(weightBuffer);
                    weights = Nd4j.create((int) dims[0], (int) dims[1]);
                    j = 0;
                    for (int i1 = 0; i1 < dims[0]; i1++)
                        for (int i2 = 0; i2 < dims[1]; i2++)
                            weights.putScalar(i1, i2, weightBuffer[j++]);
                    break;
                case 1: /* Bias */
                    weightBuffer = new float[(int) dims[0]];
                    fp = new FloatPointer(weightBuffer);
                    d.read(fp, new hdf5.DataType(hdf5.PredType.NATIVE_FLOAT()));
                    fp.get(weightBuffer);
                    weights = Nd4j.create((int) dims[0]);
                    j = 0;
                    for (int i1 = 0; i1 < dims[0]; i1++)
                        weights.putScalar(i1, weightBuffer[j++]);
                    break;
                default:
                    throw new UnsupportedKerasConfigurationException(
                            "Cannot import weights with rank " + nbDims);

                }
                if (!weightsMap.containsKey(layerName))
                    weightsMap.put(layerName, new HashMap<String, INDArray>());
                weightsMap.get(layerName).put(paramName, weights);
                d.close();
                break;
            case H5O_TYPE_GROUP:
            default:
                groups.add(g.asCommonFG().openGroup(objPtr));
                break;
            }
        }
        g.close();
    }
    file.close();
    return weightsMap;
}

From source file:org.imsglobal.caliper.request.ApacheHttpRequestorSingleMinimalEventTest.java

@Test
public void testSerializedEnvelope() throws Exception {
    // Set up Mapper; filter out nulls/empties
    ObjectMapper mapper = JsonObjectMapper.create(JsonInclude.Include.NON_EMPTY);

    // Serialize envelope, excluding null properties, empty objects and empty arrays
    String json = httpRequestor.serializeEnvelope(envelope, mapper);

    // Swap out sendTime=DateTime.now() in favor of fixture value (or test will most assuredly fail).
    Pattern pattern = Pattern.compile("\"sendTime\":\"[^\"]*\"");
    Matcher matcher = pattern.matcher(json);
    json = matcher.replaceFirst("\"sendTime\":\"" + TestDates.getDefaultSendTime() + "\"");

    String fixture = jsonFixture("fixtures/caliperEnvelopeEventViewViewedMinimal.json");
    JSONAssert.assertEquals(fixture, json, JSONCompareMode.NON_EXTENSIBLE);
}

From source file:org.infoscoop.request.proxy.ProxyConfig.java

private String replaceUrl(String url, ProxyConfigCase matchCase) {
    String resultUrl = url;//from  w ww  . ja va2  s .c  o m
    String replacement = matchCase.getReplacement();
    if (replacement != null) {
        Pattern pattern = Pattern.compile(matchCase.getPattern());
        Matcher matcher = pattern.matcher(url);
        resultUrl = matcher.replaceFirst(replacement);
        if (log.isInfoEnabled())
            log.info("Replace: " + url + " to " + resultUrl);
    }
    return resultUrl;
}

From source file:org.wso2.carbon.event.processor.core.internal.util.helper.EventProcessorHelper.java

/**
 * Sets an annotation name for a given execution plan to be true or false.
 * For example, when an execution plan has the statement "@Plan:statistics('false')" and false need to be set to true,
 * then this helper method can be used./*from ww w. ja  va 2 s . c  o m*/
 *
 * @param executionPlan        Existing execution plan, either having the annotation name set to be true/false,
 *                             or the annotation name is not present in the execution plan at all.
 * @param annotationName       The annotation name which needs to be set to true/false.
 *                             For example, in Siddhi statement @Plan:name('false'), 'name' will be the annotation name.
 * @param isAnnotationNameTrue Whether the annotation name need to be set to true or false.
 * @return New execution plan with the given plan annotation name set to be true.
 */
public static String setExecutionPlanAnnotationName(String executionPlan, String annotationName,
        boolean isAnnotationNameTrue) {
    String newExecutionPlan = null;
    String planHeader = "";
    String planBody = "";
    String planHeaderLineRegex = EventProcessorConstants.PLAN_HEADER_LINE_REGEX;

    String regexToBeReplaced = "^\\s*" + //beginning of line with zero or more whitespaces
            EventProcessorConstants.ANNOTATION_TOKEN_AT + EventProcessorConstants.ANNOTATION_PLAN
            + EventProcessorConstants.ANNOTATION_TOKEN_COLON + annotationName + "\\"
            + EventProcessorConstants.ANNOTATION_TOKEN_OPENING_BRACKET + //bracket is escaped, because the literal is meant.
            EventProcessorConstants.SIDDHI_SINGLE_QUOTE + !isAnnotationNameTrue
            + EventProcessorConstants.SIDDHI_SINGLE_QUOTE + "\\"
            + EventProcessorConstants.ANNOTATION_TOKEN_CLOSING_BRACKET; //bracket is escaped, because the literal is meant.

    String replacement = EventProcessorConstants.ANNOTATION_TOKEN_AT + EventProcessorConstants.ANNOTATION_PLAN
            + EventProcessorConstants.ANNOTATION_TOKEN_COLON + annotationName
            + EventProcessorConstants.ANNOTATION_TOKEN_OPENING_BRACKET
            + EventProcessorConstants.SIDDHI_SINGLE_QUOTE + isAnnotationNameTrue
            + EventProcessorConstants.SIDDHI_SINGLE_QUOTE
            + EventProcessorConstants.ANNOTATION_TOKEN_CLOSING_BRACKET;

    Matcher matcher = Pattern.compile(regexToBeReplaced, Pattern.MULTILINE).matcher(executionPlan);

    if (matcher.find()) { //statement with annotation name set to false, is already in the plan; In that case, false will be replaced with true.

        //finding the whitespaces given by the user before "@Plan:name()" statement and prepending those at replacement.
        String[] matchSplitArray = matcher.group().split(EventProcessorConstants.ANNOTATION_TOKEN_AT);
        String whitespaces = "";
        if (matchSplitArray.length > 1) {
            whitespaces += matchSplitArray[0];
        }

        replacement = whitespaces + replacement;
        newExecutionPlan = matcher.replaceFirst(replacement);

    } else { //statement with annotation name is not there in the plan; it'll be inserted.
        String[] planHeaderArray = executionPlan.split(EventProcessorConstants.SIDDHI_LINE_SEPARATER);
        for (int i = 0; i < planHeaderArray.length; i++) {
            if (planHeaderArray[i].matches(planHeaderLineRegex)) {
                if (planHeaderArray[i].matches(EventProcessorConstants.END_OF_PLAN_HEADER_COMMENT_REGEX)) {
                    break;
                }
                planHeader += planHeaderArray[i] + EventProcessorConstants.SIDDHI_LINE_SEPARATER;
            } else {
                break;
            }
        }
        planBody = executionPlan.replace(planHeader, "");
        newExecutionPlan = planHeader + replacement + EventProcessorConstants.SIDDHI_LINE_SEPARATER
                + EventProcessorConstants.SIDDHI_LINE_SEPARATER + planBody;
    }
    return newExecutionPlan;
}

From source file:keywhiz.auth.cookie.AuthenticatedEncryptedCookieFactoryTest.java

@Test
public void expiredCookieMatchesValidCookieExceptContent() {
    Matcher matcher = Pattern.compile("(.*session=)[^;]+(;.*)").matcher(validCookie);
    assertThat(matcher.find()).isTrue();

    String withoutSession = matcher.replaceFirst("$1expired$2");
    assertThat(withoutSession).isEqualTo(expiredCookie);
}

From source file:org.imsglobal.caliper.request.ApacheHttpRequestorSingleEventTest.java

@Test
public void testSerializedEnvelope() throws Exception {
    // Set up Mapper
    ObjectMapper mapper = JsonObjectMapper.create(JsonInclude.Include.ALWAYS);

    // Serialize envelope
    String json = httpRequestor.serializeEnvelope(envelope, mapper);

    // Swap out sendTime=DateTime.now() in favor of fixture value (or test will most assuredly fail).
    Pattern pattern = Pattern.compile("\"sendTime\":\"[^\"]*\"");
    Matcher matcher = pattern.matcher(json);
    json = matcher.replaceFirst("\"sendTime\":\"" + TestDates.getDefaultSendTime() + "\"");

    String fixture = jsonFixture("fixtures/eventStorePayload.json");
    JSONAssert.assertEquals(fixture, json, JSONCompareMode.NON_EXTENSIBLE);
}

From source file:org.mule.module.kindling.serialization.KindlingDateDeserializer.java

@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    String date = jp.getText();//from   w ww  .  j a v  a2 s  .c  om

    try {
        Matcher m = KindlingDateUtils.PATTERN_ISO8601_TO_RFC822.matcher(date);
        // Remove the : (from +dd:dd to +dddd)
        if (m.find()) {
            date = m.replaceFirst("$1$2$3");
        }
        return KindlingDateUtils.DATE_FORMAT_WITH_RFC822_TIME_ZONE.parse(date);

    } catch (ParseException e2) {
        try {
            return KindlingDateUtils.DATE_FORMAT_WITHOUT_TIME_ZONE.parse(date);
        } catch (ParseException e3) {
            logger.error(String.format("Cannot parse date (%s) to any format", date));
            return null;
        }
    }
}