List of usage examples for java.util.regex Matcher start
public int start()
From source file:com.afousan.service.RetwisRepository.java
private String replaceReplies(String content) { Matcher regexMatcher = MENTION_REGEX.matcher(content); while (regexMatcher.find()) { String match = regexMatcher.group(); int start = regexMatcher.start(); int stop = regexMatcher.end(); String uName = match.substring(1); if (isUserValid(uName)) { content = content.substring(0, start) + "<a href=\"!" + uName + "\">" + match + "</a>" + content.substring(stop); }//from w w w.ja v a2 s .c om } return content; }
From source file:com.haulmont.cuba.core.global.QueryTransformerRegex.java
@Override public void mergeWhere(String query) { int startPos = 0; Matcher whereMatcher = WHERE_PATTERN.matcher(query); if (whereMatcher.find()) startPos = whereMatcher.end() + 1; int endPos = query.length(); Matcher lastClauseMatcher = LAST_CLAUSE_PATTERN.matcher(query); if (lastClauseMatcher.find()) endPos = lastClauseMatcher.start(); addWhere(query.substring(startPos, endPos)); }
From source file:PropertiesHelper.java
/** * Adds new properties to an existing set of properties while * substituting variables. This function will allow value * substitutions based on other property values. Value substitutions * may not be nested. A value substitution will be ${property.key}, * where the dollar-brace and close-brace are being stripped before * looking up the value to replace it with. Note that the ${..} * combination must be escaped from the shell. * * @param b is the set of properties to add to existing properties. * @return the combined set of properties. *//* w w w. j a v a2 s.c om*/ protected Properties addProperties(Properties b) { // initial // Properties result = new Properties(this); Properties sys = System.getProperties(); Pattern pattern = Pattern.compile("\\$\\{[-a-zA-Z0-9._]+\\}"); for (Enumeration e = b.propertyNames(); e.hasMoreElements();) { String key = (String) e.nextElement(); String value = b.getProperty(key); // unparse value ${prop.key} inside braces Matcher matcher = pattern.matcher(value); StringBuffer sb = new StringBuffer(); while (matcher.find()) { // extract name of properties from braces String newKey = value.substring(matcher.start() + 2, matcher.end() - 1); // try to find a matching value in result properties String newVal = getProperty(newKey); // if still not found, try system properties if (newVal == null) { newVal = sys.getProperty(newKey); } // replace braced string with the actual value or empty string matcher.appendReplacement(sb, newVal == null ? "" : newVal); } matcher.appendTail(sb); setProperty(key, sb.toString()); } return this; }
From source file:de.tudarmstadt.ukp.dkpro.core.textnormalizer.frequency.ExpressiveLengtheningNormalizer.java
public String getBestReplacement(String token) throws IOException { Pattern pattern = Pattern.compile("([a-zA-Z])\\1{1,}"); Matcher matcher = pattern.matcher(token); // In case there are no abnormalities if (!matcher.find()) return token; // Collecting the start points of all abnormal parts List<Integer> abnormalities = new ArrayList<Integer>(); matcher.reset();/*from ww w. j av a 2s .co m*/ while (matcher.find()) { abnormalities.add(matcher.start()); } // splitting in parts starting with first character abnormalities List<String> parts = new ArrayList<String>(); for (int i = 0; i < abnormalities.size(); i++) { // in case the token has only one abnormality if (abnormalities.size() == 1) { parts.add(token); break; } // first abnormality if (i == 0) { parts.add(token.substring(0, abnormalities.get(i + 1))); continue; } // last abnormality if (i == abnormalities.size() - 1) { parts.add(token.substring(abnormalities.get(i))); continue; } if (i < abnormalities.size() - 1) { parts.add(token.substring(abnormalities.get(i), abnormalities.get(i + 1))); continue; } } // Fills big list of arrays with all parts and their versions List<String[]> bigList = new ArrayList<String[]>(); for (String part : parts) { String v1 = part.replaceFirst(pattern.pattern(), "$1"); String v2 = part.replaceFirst(pattern.pattern(), "$1$1"); String v3 = part.replaceFirst(pattern.pattern(), "$1$1$1"); bigList.add(new String[] { v1, v2, v3 }); } List<String> candidates = permute(bigList, 0, new ArrayList<String>(), ""); return getMostFrequentCandidate(candidates); }
From source file:hu.petabyte.redflags.engine.gear.indicator.hu.TechCapRefCondExceedEstimValIndicator.java
@Override protected IndicatorResult flagImpl(Notice notice) { String s = String.format("%s", // fetchTechnicalCapacity(notice) // ).trim();// w w w .j a va 2 s .com String contractType = fetchContractType(notice); long estimVal = fetchEstimatedValue(notice); String estimCurr = fetchEstimatedValueCurrency(notice); if (contractType.isEmpty() || 0 == estimVal || null == estimCurr) { return missingData(); } if (estimCurr.matches("Ft|forint")) { estimCurr = "HUF"; } long sum = 0; for (String line : s.split("\n")) { if (HuIndicatorHelper.isRef(line) || line.matches("[0-9]\\. rsz( eset)?ben.*")) { // exceptions I line = line.replaceAll( "legalbb.*?(rtk|rtket elr).*?beruhzs.*?(irnyul|kapcsold|vonatkoz)", "[CUT]"); line = line.replaceAll(".*szerzdsek kzl legalbb.*", "[CUT]"); line = line.replaceAll(".*ptsekor vgzett.*mszaki ellenr.*", "[CUT]"); if ("NC-4".equals(contractType)) { line = line.replaceAll( "kivitelezsi munk.*?sszrtk.*?(?<v>\\d{1,3}( ?\\d{3}){2,10})(?<c> [A-Za-z]+)[^A-Za-z].*", "[CUT]"); } line = line.replaceAll( ".*(megvalstshoz|beruhzshoz) kapcsold (mrnki tancsads|mszaki ellenri tevkenysg).*", "[CUT]"); line = line.replaceAll(".*hitel.*?(nyjts|finanszrozs).*?referenci.*", "[CUT]"); Matcher m = valuePattern.matcher(line); long mv = 0; while (m.find()) { // exceptions II String before = line.substring(0, m.start()); int beforeLen = before.length(); before = before.replaceAll(".*[,;.]", ""); int commaBefore = beforeLen - before.length(); String part = line.substring(commaBefore).split("[,;.]")[0]; // REV 746 --- if ("NC-4".equals(contractType) && part.matches( ".*sszrtk.*megvalsu(lt|l) ptsi szerzds teljests.*")) { continue; } if (part.matches( ".*(kivitelezsi|beruhzsi|ptsi) rtket (elr|meghalad).*projektre vonatkoz beruhzs.*")) { continue; } if (part.matches(".*(beruhzs rtke|rtk projekt).*")) { continue; } // NEW if ("NC-1".equals(contractType) && part.matches( ".*sszrtk.*?(ltestmny|intzmny).*?(pts|feljts|megvalsts).*?(irnyul|kapcsold|vonatkoz).*")) { continue; } if ("NC-4".equals(contractType) && part.matches(".*kivitelezs.*? rtke.*")) { continue; } if ("NC-4".equals(contractType) && part.matches(".*teljestst elr.*")) { continue; } if (part.matches( ".*(kltsgvetsi fssze(g|st)|sajt tk|rbevtel|pnzforgalom).*")) { continue; } if (part.contains("beruhzsi rtk")) { continue; } if (part.matches(".*rtk .*?beruhzs.*")) { continue; } if (part.matches(".*beruhzs\\(?ok\\)? sszes rtke.*")) { continue; } if (part.contains("projekttel kapcsolat")) { continue; } if (part.matches(".*projekt(\\(?ek\\)?)? (ssz)?rtke.*")) { continue; } if (part.contains("projekt sszkltsg")) { continue; } if (part.contains("projekt kltsgvets")) { continue; } if (part.contains("biztostsi sszeg")) { continue; } System.out.println("*** " + line); System.out.println(" > " + part); // parse long v = Long.parseLong("0" + m.group("v").replaceAll(" ", "")); String c = m.group("c").trim(); if (c.matches("Ft|forint.*")) { c = "HUF"; } if ("eFt".equals(c)) { v *= 1000; c = "HUF"; } if (c.equals(estimCurr)) { // sum += v; mv = Math.max(mv, v); } } sum += mv; } } LOG.trace("Estimated value: {}, ref cond value: {}", estimVal, sum); if (sum > estimVal) { return returnFlag("info", "ref=" + (sum / 1000000), "estim=" + (estimVal / 1000000), "curr=" + estimCurr); } // credits long credit = 0L; long creditRef = 0L; if (!notice.getObjs().isEmpty()) { String tq = "" + notice.getObjs().get(0).getTotalQuantity(); for (String line : tq.split("\n")) { if (line.toLowerCase().contains("hitel")) { Matcher m = creditPattern.matcher(line); if (m.find()) { credit = Math.max(credit, Long.parseLong("0" + m.group("v").replaceAll(" ", ""))); } } } } if (0 < credit) { for (String line : s.split("\n")) { if ((HuIndicatorHelper.isRef(line) || line.matches("[0-9]\\. rsz( eset)?ben")) && line.matches(".*hitel.*?(nyjts|finanszrozs).*") && !line.matches(".*kltsgvetsi.*")) { Matcher m = valuePattern.matcher(line); if (m.find()) { creditRef += Long.parseLong("0" + m.group("v").replaceAll(" ", "")); } } } } LOG.trace("Credit: {}, ref cond for credit: {}", credit, creditRef); if (0 < credit && credit < creditRef) { return returnFlag("info", "ref=" + (sum / 1000000), "estim=" + (estimVal / 1000000), "curr=" + estimCurr); } return null; }
From source file:com.haulmont.yarg.loaders.impl.AbstractDbDataLoader.java
protected QueryPack prepareQuery(String query, BandData parentBand, Map<String, Object> reportParams) { Map<String, Object> currentParams = new HashMap<String, Object>(); if (reportParams != null) { currentParams.putAll(reportParams); }/*w ww .j a v a 2 s. co m*/ //adds parameters from parent bands hierarchy while (parentBand != null) { addParentBandDataToParameters(parentBand, currentParams); parentBand = parentBand.getParentBand(); } List<QueryParameter> queryParameters = new ArrayList<QueryParameter>(); HashSet<String> paramNames = findParameterNames(query); Map<String, String> paramsToRemoveFromQuery = new LinkedHashMap<String, String>(); for (String paramName : paramNames) { Object paramValue = currentParams.get(paramName); String alias = "${" + paramName + "}"; String paramNameRegexp = "\\$\\{" + paramName + "\\}"; String valueRegexp = "([\\w|\\d|\\.|\\_]+|\'.+?\'|\".+?\"|\\(.+?\\))";//fieldName|literal|list or sub-query String andRegexp = "\\s+and\\s+"; String orRegexp = "\\s+or\\s+"; String operatorRegexp = "(=|>=|<=|\\slike\\s|>|<|\\sin\\s)"; String expression1Rgxp = "\\s*" + valueRegexp + "\\s*" + operatorRegexp + "\\s*" + paramNameRegexp + "\\s*"; String expression2Rgxp = "\\s*" + paramNameRegexp + "\\s*" + operatorRegexp + "\\s*" + valueRegexp + "\\s*"; String expressionRgxp = "(" + expression1Rgxp + "|" + expression2Rgxp + ")"; String andFirstRgxp = andRegexp + expressionRgxp; String orFirstRgxp = orRegexp + expressionRgxp; String andLastRgxp = expressionRgxp + andRegexp; String orLastRgxp = expressionRgxp + orRegexp; String isNullRgxp = paramNameRegexp + "\\s+is\\s+null"; String isNotNullRgxp = paramNameRegexp + "\\s+is\\s+not\\s+null"; String boundsRegexp = "\\[\\[.+?" + paramNameRegexp + ".+?\\]\\]"; if (paramValue == null && reportParams != null && reportParams.containsKey(paramName)) {//if value == null && this is user parameter - remove condition from query paramsToRemoveFromQuery.put("(?i)" + andFirstRgxp, " and 1=1 "); paramsToRemoveFromQuery.put("(?i)" + andLastRgxp, " 1=1 and "); paramsToRemoveFromQuery.put("(?i)" + orFirstRgxp, " or 1=0 "); paramsToRemoveFromQuery.put("(?i)" + orLastRgxp, " 1=0 or "); paramsToRemoveFromQuery.put("(?i)" + expressionRgxp, " 1=1 "); paramsToRemoveFromQuery.put("(?i)" + isNullRgxp, " 1=1 "); paramsToRemoveFromQuery.put("(?i)" + isNotNullRgxp, " 1=0 "); paramsToRemoveFromQuery.put("(?i)" + boundsRegexp, " "); } else if (query.contains(alias)) {//otherwise - create parameter and save each entry's position Pattern pattern = Pattern.compile(paramNameRegexp); Matcher replaceMatcher = pattern.matcher(query); int subPosition = 0; while (replaceMatcher.find(subPosition)) { subPosition = replaceMatcher.start(); queryParameters .add(new QueryParameter(paramNameRegexp, subPosition, convertParameter(paramValue))); subPosition = replaceMatcher.end(); } } } for (Map.Entry<String, String> entry : paramsToRemoveFromQuery.entrySet()) { query = query.replaceAll(entry.getKey(), entry.getValue()); } query = query.replaceAll("\\[\\[", ""); query = query.replaceAll("\\]\\]", ""); // Sort params by position Collections.sort(queryParameters, new Comparator<QueryParameter>() { @Override public int compare(QueryParameter o1, QueryParameter o2) { return o1.getPosition().compareTo(o2.getPosition()); } }); //normalize params position to 1..n for (int i = 1; i <= queryParameters.size(); i++) { QueryParameter queryParameter = queryParameters.get(i - 1); queryParameter.setPosition(i); } for (QueryParameter parameter : queryParameters) { query = insertParameterToQuery(query, parameter); } return new QueryPack(query.trim().replaceAll(" +", " "), queryParameters.toArray(new QueryParameter[queryParameters.size()])); }
From source file:com.github.tteofili.p2h.DataSplitTest.java
@Ignore @Test// ww w . ja v a 2 s . com public void testDataSplit() throws Exception { String regex = "\\n\\d(\\.\\d)*\\.?\\u0020[\\w|\\-|\\|\\:]+(\\u0020[\\w|\\-|\\:|\\]+){0,10}\\n"; String prefix = "/path/to/h2v/"; Pattern pattern = Pattern.compile(regex); Path path = Paths.get(getClass().getResource("/papers/raw/").getFile()); File file = path.toFile(); if (file.exists() && file.list() != null) { for (File doc : file.listFiles()) { String s = IOUtils.toString(new FileInputStream(doc)); String docName = doc.getName(); File fileDir = new File(prefix + docName); assert fileDir.mkdir(); Matcher matcher = pattern.matcher(s); int start = 0; String sectionName = "abstract"; while (matcher.find(start)) { String string = matcher.group(0); if (isValid(string)) { String content; if (start == 0) { // abstract content = s.substring(0, matcher.start()); } else { content = s.substring(start, matcher.start()); } File f = new File(prefix + docName + "/" + docName + "_" + sectionName); assert f.createNewFile() : "could not create file" + f.getAbsolutePath(); FileOutputStream outputStream = new FileOutputStream(f); IOUtils.write(content, outputStream); start = matcher.end(); sectionName = string.replaceAll("\n", "").trim(); } else { start = matcher.end(); } } // remaining File f = new File(prefix + docName + "/" + docName + "_" + sectionName); assert f.createNewFile(); FileOutputStream outputStream = new FileOutputStream(f); IOUtils.write(s.substring(start), outputStream); } } }
From source file:com.logsniffer.reader.support.FormattedTextReader.java
/** * @param formatPattern//from ww w . j ava 2 s. co m * the formatPattern to set */ @Override protected void init() throws FormatException { super.init(); if (parsingPattern == null) { // Only if not yet parsed if (formatPattern != null) { final ArrayList<Specifier> specs = new ArrayList<Specifier>(); final StringBuilder parsingPatternStr = new StringBuilder(); final Matcher m = SPECIFIER_PATTERN.matcher(formatPattern); int leftPos = 0; while (m.find()) { if (m.start() > leftPos) { parsingPatternStr.append(Pattern.quote(formatPattern.substring(leftPos, m.start()))); } leftPos = m.end(); final String minWidthStr = m.group(2); final String maxWidthStr = m.group(4); final String specName = m.group(5); final String specModifier = m.group(7); int minWidth = -1; if (minWidthStr != null && minWidthStr.length() > 0) { minWidth = Integer.parseInt(minWidthStr); } int maxWidth = -1; if (maxWidthStr != null && maxWidthStr.length() > 0) { maxWidth = Integer.parseInt(maxWidthStr); } Specifier spec = null; for (final Specifier specTest : createSupportedSpecifiers()) { if (specTest.getSpecifierKey().equals(specName)) { spec = specTest; break; } } if (spec == null) { logger.debug( "Format specifier {} in pattern '{}' is unknown and will be parsed as simple text pattern", specName, formatPattern); spec = new ArbitraryTextSpecifier(specName, false); } else if (spec instanceof IgnoreSpecifier) { logger.debug("Format specifier '{}' in pattern '{}' is ignored", specName, formatPattern); continue; } spec.setMaxWidth(maxWidth); spec.setMinWidth(minWidth); spec.setModifier(specModifier); parsingPatternStr.append("("); parsingPatternStr.append(spec.getRegex()); parsingPatternStr.append(")"); spec.fieldName = specifiersFieldMapping.get(specName); if (StringUtils.isBlank(spec.fieldName)) { spec.fieldName = specName; } specs.add(spec); } parsingPatternStr.append(Pattern.quote(formatPattern.substring(leftPos))); parsingPattern = Pattern.compile(parsingPatternStr.toString()); parsingSpecifiers = specs.toArray(new Specifier[specs.size()]); logger.debug("Prepared parsing pattern '{}' for log4j conversion pattern: {}", parsingPattern, formatPattern); } else { parsingSpecifiers = null; parsingPattern = null; } } }
From source file:nya.miku.wishmaster.chans.cirno.MikubaModule.java
@Override public String sendPost(SendPostModel model, ProgressListener listener, CancellableTask task) throws Exception { String body, video;//from www. j av a 2 s . c om Matcher ytLink = YOUTUBE_PATTERN.matcher(model.comment); if (ytLink.find()) { body = new StringBuilder(model.comment).delete(ytLink.start(), ytLink.end()).toString(); video = "http://www.youtube.com/watch?v=" + ytLink.group(1); } else { body = model.comment; video = ""; } String url = (useHttps() ? "https://" : "http://") + MIKUBA_DOMAIN + "/reply/" + (model.threadNumber == null ? "0" : model.threadNumber); ExtendedMultipartBuilder postEntityBuilder = ExtendedMultipartBuilder.create().setDelegates(listener, task) .addString("title", model.subject).addString("captcha", model.captchaAnswer).addString("body", body) .addString("video", video).addString("email", ""); if (model.attachments != null && model.attachments.length > 0) postEntityBuilder.addFile("image", model.attachments[0], model.randomHash); else postEntityBuilder.addPart("image", new ByteArrayBody(new byte[0], "")); HttpRequestModel request = HttpRequestModel.builder().setPOST(postEntityBuilder.build()).setNoRedirect(true) .build(); HttpResponseModel response = null; try { response = HttpStreamer.getInstance().getFromUrl(url, request, httpClient, null, task); Logger.d(TAG, response.statusCode + " - " + response.statusReason); if (response.statusCode == 303) { return null; } else if (response.statusCode == 200) { ByteArrayOutputStream output = new ByteArrayOutputStream(1024); IOUtils.copyStream(response.stream, output); String htmlResponse = output.toString("UTF-8"); //Logger.d(TAG, htmlResponse); if (htmlResponse.contains("/static/captcha.gif")) { throw new Exception(" ?!"); } } else throw new Exception(response.statusCode + " - " + response.statusReason); } finally { if (response != null) response.release(); saveCookiesToPreferences(); } return null; }
From source file:com.google.enterprise.connector.filesystem.FileConnectorTypeTest.java
/** * Make sure HTML tags in {@code s} are balanced. * * @param s//from ww w.j a va 2 s .co m */ private void assertBalancedTags(String s) { LinkedList<String> stack = Lists.newLinkedList(); Matcher m = TAG.matcher(s); int start = 0; while (m.find(start)) { String tag = s.substring(m.start(), m.end()); if (isOpenTag(tag)) { stack.addFirst(tag); } else if (isCloseTag(tag)) { String open = stack.poll(); assertNotNull(String.format("extra tag: %s", tag), open); assertEquals(String.format("mismatched tags: %s vs %s", open, tag), getName(open), getName(tag)); } else { // Ignore open-closed tags (<tag/>). } start = m.end(); } assertEquals("Open tags at end of input", 0, stack.size()); }