List of usage examples for java.util.regex Matcher end
public int end(String name)
From source file:com.nextep.designer.sqlgen.oracle.parser.OraclePackageParser.java
/** * Renames the provided SQL string by the new name considering that the name will follow the * supplied token/*from w w w . j av a2s. c o m*/ * * @param sql entire SQL source to rename * @param newName new name to inject in the SQL source * @param firstTokenBeforeName first token before the name information * @return the full SQL, renamed */ private String renameSqlHeader(String sql, String newName, String firstTokenBeforeName) { // Extracting procedure or function name from the SQL source Pattern pattern = Pattern.compile("\\s*(" + firstTokenBeforeName + ")\\s+((\\w)+)"); //$NON-NLS-1$ //$NON-NLS-2$ String lowerCased = sql.toLowerCase(); Matcher m = pattern.matcher(lowerCased); String newSql = sql; // Looking for first occurrence if (m.find()) { // Building the "renamed" SQL source declaration newSql = sql.substring(0, m.start(2)) + newName + sql.substring(m.end(2)); } return newSql; }
From source file:com.phoenixnap.oss.ramlapisync.javadoc.JavaDocEntry.java
/** * Removes {@link or other {@ notation from the javadoc and retains the enclosed data * //from w w w .java 2s . c o m * @param target The target comment to clean * @return The comment cleaned from the target characters */ private String cleanLinks(String target) { Matcher linkMatcher = LINK_BLOCK.matcher(target); // Build the main comment first while (linkMatcher.find()) { try { target = target.substring(0, linkMatcher.start(0)) + linkMatcher.group(1) + target.substring(linkMatcher.end(1) + 1); } catch (Exception ex) { //do nothing } } return target; }
From source file:com.chiorichan.factory.parsers.BasicParser.java
public String runParser(String source) throws Exception { if (source == null || source.isEmpty()) return ""; Matcher m1 = p1.matcher(source); Matcher m2 = p2.matcher(source); while (m1.find() && m2.find()) { String[] args = m1.group(1).split("[ ]?,[ ]?"); String[] args2 = new String[args.length + 1]; args2[0] = m1.group(0);//from ww w .j a va2 s . c om for (int i = 0; i < args.length; i++) args2[i + 1] = StringFunc.trimAll(args[i].trim(), '"'); String result = resolveMethod(args2); if (result == null) result = ""; source = new StringBuilder(source).replace(m2.start(1), m2.end(1), result).toString(); // We have to reset the matcher since the source changes with each loop m1 = p1.matcher(source); m2 = p2.matcher(source); } return source; }
From source file:net.erdfelt.android.sdkfido.util.PropertyExpander.java
public String expand(String str) { if (StringUtils.isEmpty(str)) { // Empty string. Fail fast. return str; }// w w w . ja va 2s .co m if (str.indexOf("@") < 0) { // Contains no potential expressions. Fail fast. return str; } Matcher mat = pat.matcher(str); int offset = 0; String expression; String value; StringBuffer expanded = new StringBuffer(); while (mat.find(offset)) { expression = mat.group(1); expanded.append(str.substring(offset, mat.start(1))); value = props.get(mat.group(2)); if (value != null) { expanded.append(value); } else { expanded.append(expression); } offset = mat.end(1); } expanded.append(str.substring(offset)); if (expanded.indexOf("@@") >= 0) { // Special case for escaped content. return expanded.toString().replaceAll("\\@\\@", "\\@"); } else { // return expanded return expanded.toString(); } }
From source file:mobi.jenkinsci.server.core.net.ProxyUtil.java
private String resolveImages(final String userAgent, final String pluginName, final Pattern imgLinkPattern, final int index, final String url, final String resultString) { log.debug("Resolving images for URL " + url); final StringBuilder outString = new StringBuilder(); int currPos = 0; final Matcher matcher = imgLinkPattern.matcher(resultString); while (matcher.find(currPos)) { final int start = matcher.start(index); final int end = matcher.end(index); final String imagePath = matcher.group(index); if (isAlreadyDataEncoded(imagePath)) { continue; }//www. j a v a 2s . co m outString.append(resultString.substring(currPos, start)); try { outString.append(retrieveImage(userAgent, pluginName, url, imagePath)); } catch (final Exception e) { log.warn("Cannot retrieve image '" + imagePath + "'", e); } currPos = end; } outString.append(resultString.substring(currPos)); log.debug(outString.length() + " Base64 of images included for URL " + url); return outString.toString(); }
From source file:com.alkacon.opencms.formgenerator.CmsTableField.java
/** * Reads from the default value the configuration of the rows and columns and fills * it with the values from the parameter if its exists.<p> * //from www . j a va 2 s.c o m * @param defaultValue the default value with the configuration of the rows and columns * @param parameter the map of the requested parameter * * @throws CmsConfigurationException if no rows or columns are defined */ public void parseDefault(String defaultValue, Map<String, String[]> parameter) throws CmsConfigurationException { m_tableItems = new HashMap<String, CmsFieldItem>(); // check if the default value is empty if (CmsStringUtil.isEmptyOrWhitespaceOnly(defaultValue)) { throw new CmsConfigurationException( Messages.get().container(Messages.ERR_INIT_INPUT_FIELD_MISSING_ITEM_2, getName(), getType())); } String backend = defaultValue; String frontend = defaultValue; // parse the default value, it should look like '%(ColumnA,ColumnB|RowA,RowB)dbcola,dbcolb|dbrowa,dbrowb' Matcher regex = Pattern.compile("^(%\\()(.*)(\\)).*").matcher(backend); if (regex.matches()) { // a frontend exists frontend = regex.group(2); backend = backend.substring(regex.end(2) + 1, backend.length()); if (CmsStringUtil.isEmpty(backend)) { backend = frontend; } } else { frontend = backend; } List<String> cells = CmsStringUtil.splitAsList(frontend, "|"); List<String> dbcells = CmsStringUtil.splitAsList(backend, "|"); // get the columns and rows from the default value List<String> testRow = new ArrayList<String>(); List<String> testCol = new ArrayList<String>(); m_cols = CmsStringUtil.splitAsList(cells.get(0), ","); m_dbcols = CmsStringUtil.splitAsList(dbcells.get(0), ",", true); m_rows = CmsStringUtil.splitAsList(cells.get(1), ","); m_dbrows = CmsStringUtil.splitAsList(dbcells.get(1), ",", true); // test if the frontend and backend columns are in the size identical if (m_cols.size() != m_dbcols.size()) { throw new CmsConfigurationException(Messages.get().container(Messages.ERR_INIT_TABLE_FIELD_UNEQUAL_0)); } // test if the frontend and backend rows are in the size identical if (m_rows.size() != m_dbrows.size()) { throw new CmsConfigurationException(Messages.get().container(Messages.ERR_INIT_TABLE_FIELD_UNEQUAL_0)); } for (int i = 0; i < m_dbrows.size(); i++) { // look if the row not already exists String dbrow = m_dbrows.get(i); if (testRow.contains(dbrow)) { throw new CmsConfigurationException( Messages.get().container(Messages.ERR_INIT_TABLE_FIELD_UNIQUE_1, dbrow)); } // for each column generate the item for (int j = 0; j < m_dbcols.size(); j++) { // look if the column not already exists String dbcol = m_dbcols.get(j); if ((i == 0) && testCol.contains(dbcol)) { throw new CmsConfigurationException( Messages.get().container(Messages.ERR_INIT_TABLE_FIELD_UNIQUE_1, dbcol)); } // get the parameter of the cell String key = getKey(dbcol, dbrow, false); Object param = parameter.get(getName() + key); String[] value = new String[] { "" }; if (param != null) { value = (String[]) param; } // add the cell m_tableItems.put(key, new CmsFieldItem(value[0], getKey(dbcol, dbrow, true), key, false, false)); testCol.add(dbcol); } testRow.add(dbrow); } if (m_tableItems.size() <= 0) { throw new CmsConfigurationException( Messages.get().container(Messages.ERR_INIT_INPUT_FIELD_MISSING_ITEM_2, getName(), getType())); } }
From source file:io.sugo.grok.api.Grok.java
/** * Match the given <tt>text</tt> with the named regex * {@code Grok} will extract data from the string and get an extence of {@link Match}. * * @param text : Single line of log// w w w . ja v a2 s.c om * @return Grok Match */ public Match match(String text) { if (compiledNamedRegex == null || StringUtils.isBlank(text)) { return Match.EMPTY; } Matcher m = compiledNamedRegex.matcher(text); Match match = new Match(); if (m.find()) { match.setSubject(text); match.setGrok(this); match.setMatch(m); match.setStart(m.start(0)); match.setEnd(m.end(0)); } return match; }
From source file:com.joliciel.talismane.tokeniser.filters.TokenRegexFilterImpl.java
@Override public Set<TokenPlaceholder> apply(String text) { Set<TokenPlaceholder> placeholders = new HashSet<TokenPlaceholder>(); Matcher matcher = this.getPattern().matcher(text); int lastStart = -1; while (matcher.find()) { int start = matcher.start(groupIndex); if (start > lastStart) { int end = matcher.end(groupIndex); String newText = RegexUtils.getReplacement(replacement, text, matcher); TokenPlaceholder placeholder = this.tokeniserFilterService.getTokenPlaceholder(start, end, newText, regex);/*from w ww . ja v a 2 s . c om*/ placeholder.setPossibleSentenceBoundary(this.possibleSentenceBoundary); for (String key : attributes.keySet()) placeholder.addAttribute(key, attributes.get(key)); placeholders.add(placeholder); } lastStart = start; } return placeholders; }
From source file:me.ryanhamshire.griefprevention.DataStore.java
/** * Returns a list with all links contained in the input *//*from www.ja v a 2s. co m*/ public static List<String> extractUrls(String text) { List<String> containedUrls = new ArrayList<String>(); String urlRegex = "((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)"; Pattern pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE); Matcher urlMatcher = null; try { urlMatcher = pattern.matcher(text); } catch (Throwable t) { return containedUrls; } while (urlMatcher.find()) { containedUrls.add(text.substring(urlMatcher.start(0), urlMatcher.end(0))); } return containedUrls; }
From source file:ai.susi.mind.SusiAction.java
/** * Action descriptions are templates for data content. Strings may refer to arguments from * a thought deduction using variable templates. I.e. "$name$" inside an action string would * refer to an data entity in an thought argument which has the name "name". Applying the * Action to a thought will instantiate such variable templates and produces a new String * attribute named "expression"/*w w w . j a va 2s. c o m*/ * @param thoughts an argument from previously applied inferences * @return the action with the attribute "expression" instantiated by unification of the thought with the action */ public SusiAction execution(SusiArgument thoughts, SusiMind mind, String client) { if ((this.getRenderType() == RenderType.answer || this.getRenderType() == RenderType.self) && this.json.has("phrases")) { // transform the answer according to the data ArrayList<String> a = getPhrases(); String phrase = a.get(random.nextInt(a.size())); String expression = thoughts.unify(phrase); if (expression != null) { // transform the answer according to the data // this is the final chance that we can add another thought according to a memorizing skill in the answer string Matcher m; // self-referrer evaluate contents from the answers expressions as recursion: susi is asked again while (new TimeoutMatcher(m = self_referrer.matcher(expression)).matches()) { String observation = m.group(1); expression = expression.substring(0, m.start(1) - 1) + mind.react(observation, client, new SusiThought()) + expression.substring(m.end(1) + 1); } // assignments set variables from the result expressions. These can be visible or invisible while (new TimeoutMatcher(m = visible_assignment.matcher(expression)).matches()) { String observation = m.group(1); String variable = m.group(2); expression = expression.substring(0, m.end(1)) + expression.substring(m.end(2)); // write the variable v as side-effect into the thoughts argument thoughts.think(new SusiThought().addObservation(variable, observation)); } while (new TimeoutMatcher(m = blind_assignment.matcher(expression)).matches()) { String observation = m.group(1); String variable = m.group(2); expression = expression.substring(0, m.start(1) - 1) + expression.substring(m.end(2)); // write the variable v as side-effect into the thoughts argument thoughts.think(new SusiThought().addObservation(variable, observation)); } // find an response type: self-recursion or answer if (this.getRenderType() == RenderType.answer) { // the expression is answered to the communication partner this.json.put("expression", expression); } if (this.getRenderType() == RenderType.self) { // recursive call susi with the answer expression = mind.react(expression, client, new SusiThought()); this.json.put("expression", expression); this.phrasesCache = null; // important, otherwise the expression is not recognized // patch the render type this.json.put("type", RenderType.answer.name()); this.renderTypeCache = RenderType.answer; } } } if (this.getRenderType() == RenderType.websearch && this.json.has("query")) { this.json.put("query", thoughts.unify(getStringAttr("query"))); } if (this.getRenderType() == RenderType.anchor && this.json.has("link") && this.json.has("text")) { this.json.put("link", thoughts.unify(getStringAttr("link"))); this.json.put("text", thoughts.unify(getStringAttr("text"))); } if (this.getRenderType() == RenderType.map && this.json.has("latitude") && this.json.has("longitude") && this.json.has("zoom")) { this.json.put("latitude", thoughts.unify(getStringAttr("latitude"))); this.json.put("longitude", thoughts.unify(getStringAttr("longitude"))); this.json.put("zoom", thoughts.unify(getStringAttr("zoom"))); } return this; }