List of usage examples for java.util.regex Matcher end
public int end()
From source file:net.sf.zekr.engine.search.tanzil.RegexUtils.java
License:asdf
/** * Translate a symbolic regular expression into a legal one. * /*from www. j a v a 2 s . c om*/ * @param str symbolic regex * @return legal regex */ public static final String regTrans(String str) { StringBuffer ret = new StringBuffer(); Matcher matcher = REGTRANS_PATTERN.matcher(str); int lastEnd = 0; while (matcher.find()) { String group = matcher.group(1); String replacement; if (GROUPS.containsKey(group)) replacement = (String) GROUPS.get(group); else if (CHARS.containsKey(group)) replacement = ((Character) CHARS.get(group)).toString(); else continue; ret.append(str.substring(lastEnd, matcher.start())); ret.append(replacement); lastEnd = matcher.end(); } ret.append(str.substring(lastEnd)); return ret.toString(); }
From source file:com.edgenius.wiki.render.RenderUtil.java
/** * @param input/* ww w. j a v a2s. c o m*/ * @param key * @param regions * @param newlineKey * @return */ public static CharSequence createRegion(CharSequence input, String key, Collection<Region> regions, String newlineKey) { if (regions == null || regions.size() == 0) return input; //first, web split whole text by special border tag string some think like "key_regionKey_S|E" input = createRegionBorder(input, key, regions, newlineKey); //then we split them one by one. The split is dependent on the RegionComparator(), which ensure the split //from end to start, and from inside to outside. And this makes easier on below replacement process. Set<Region> sortRegions = new TreeSet<Region>(new RegionComparator()); sortRegions.addAll(regions); StringBuilder buf = new StringBuilder(input); StringBuilder regKey = new StringBuilder(key); int ks = key.length(); for (Region region : sortRegions) { //See our issue http://bug.edgenius.com/issues/34 //and SUN Java bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6337993 Pattern pat = Pattern.compile(new StringBuilder(key).append(region.getKey()).append("S(.*?)") .append(key).append(region.getKey()).append("E").toString(), Pattern.DOTALL); try { Matcher matcher = pat.matcher(buf); if (matcher.find()) { region.setBody(matcher.group(1)); buf.delete(matcher.start(), matcher.end()); regKey.delete(ks, regKey.length()); buf.insert(matcher.start(), regKey.append(region.getKey()).append(Region.REGION_SUFFIX)); } } catch (StackOverflowError e) { AuditLogger.error("StackOverflow Error in RenderUtil.createRegion. Input[" + buf + "] Pattern [" + pat.pattern() + "]"); } catch (Throwable e) { AuditLogger.error("Unexpected error in RenderUtil.createRegion. Input[" + buf + "] Pattern [" + pat.pattern() + "]", e); } } return buf; }
From source file:com.github.abel533.mapperhelper.EntityHelper.java
/** * ?// w w w. j a v a 2s. c om */ public static String underlineToCamelhump(String str) { Matcher matcher = Pattern.compile("_[a-z]").matcher(str); StringBuilder builder = new StringBuilder(str); for (int i = 0; matcher.find(); i++) { builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase()); } if (Character.isUpperCase(builder.charAt(0))) { builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0)))); } return builder.toString(); }
From source file:org.cleverbus.common.Strings.java
/** * Removes the HTML sequences from an input string. * * @param str the input string/*from www . j ava2s . co m*/ * @return the char sequence without HTML */ @Nullable public static CharSequence removeHtml(@Nullable String str) { if (!isEmpty(str)) { StringBuilder sb = new StringBuilder(); Matcher m = HTML_REMOVE_PATTERN.matcher(str); int last = 0; while (m.find()) { sb.append(str.substring(last, m.start()).replace('"', '\'')); last = m.end(); } sb.append(str.substring(last)); return sb; } return str; }
From source file:agilejson.JSON.java
public static String deCamelCase(String s) { Matcher m = decamelcasePattern.matcher(s.substring(1)); if (!m.find()) { return s.toLowerCase(); }/*w ww .j av a 2s . co m*/ String res = String.valueOf(Character.toLowerCase(s.charAt(0))); int lastEnd; while (true) { res += m.group(1); res += "_" + m.group(2).toLowerCase(); lastEnd = m.end(); if (!m.find()) { return res + s.substring(lastEnd + 1); } } }
From source file:com.logsniffer.util.grok.Grok.java
/** * Compiles a grok pattern and generates an internal standard pattern * representation for it./*from w w w . j a va 2s.c om*/ * * @param registry * Groks registry for predefined types * @param pattern * the grok pattern * @param flags * flags corresponding to {@link Pattern#flags()} * @return a compiled grok pattern * @throws GrokException */ @SuppressWarnings("unchecked") public static Grok compile(final GroksRegistry registry, final String pattern, final int flags) throws GrokException { final Grok g = new Grok(); g.grokPattern = pattern; final StringBuilder compiledPattern = new StringBuilder(); final Matcher m = PATTERN_SUBGROK.matcher(pattern); int lastPos = 0; g.typeConverters = new HashMap<>(); while (m.find()) { final String left = pattern.substring(lastPos, m.start()); lastPos = m.end(); compiledPattern.append(left); int groupsCount = PatternHelper.countOpenParens(compiledPattern.toString(), compiledPattern.length()); final String subGrokName = m.group(1); final String subGrokAttr = m.group(2); String subGrokType = m.group(3); final Grok subGrok = registry.getGroks().get(subGrokName); if (subGrok == null) { throw new GrokException("No predefined Grok pattern for name '" + subGrokName + "' found used in pattern: " + pattern); } if (subGrokAttr != null) { compiledPattern.append("("); groupsCount++; g.groupNames.put(subGrokAttr, groupsCount); } if (subGrokType != null) { subGrokType = subGrokType.toLowerCase(); if (supportedTypeConverters.containsKey(subGrokType)) { g.typeConverters.put(groupsCount, (TypeConverter<Object>) supportedTypeConverters.get(subGrokType)); } else { LOGGER.warn("Conversion type {} not support in grok pattern: {}", subGrokType, m.group(0)); } } compiledPattern.append(subGrok.regexPattern.pattern()); if (subGrokAttr != null) { compiledPattern.append(")"); } for (final String subGrokSubAttr : subGrok.groupNames.keySet()) { final int subGrokGroup = subGrok.groupNames.get(subGrokSubAttr); g.groupNames.put(subGrokSubAttr, groupsCount + subGrokGroup); if (subGrok.typeConverters.get(subGrokGroup) != null) { g.typeConverters.put(groupsCount + subGrokGroup, subGrok.typeConverters.get(subGrokGroup)); } } } compiledPattern.append(pattern.substring(lastPos)); // g.regexPattern = Pattern.compile(compiledPattern.toString(), flags); final com.google.code.regexp.Pattern namedPattern = com.google.code.regexp.Pattern .compile(compiledPattern.toString(), flags); g.regexPattern = namedPattern.pattern(); for (final String name : namedPattern.groupInfo().keySet()) { g.groupNames.put(name, namedPattern.groupInfo().get(name).get(0).groupIndex() + 1); } // Order groups by occurrence final List<Entry<String, Integer>> groups = new ArrayList<>(g.groupNames.entrySet()); Collections.sort(groups, new Comparator<Entry<String, Integer>>() { @Override public int compare(final Entry<String, Integer> o1, final Entry<String, Integer> o2) { return o1.getValue().compareTo(o2.getValue()); } }); g.groupNames.clear(); for (final Entry<String, Integer> entry : groups) { g.groupNames.put(entry.getKey(), entry.getValue()); } LOGGER.debug("Compiled grok: {}", g); return g; }
From source file:com.google.gdt.eclipse.designer.uibinder.parser.UiBinderContext.java
/** * In tests we use "wbp:name" attribute to access widgets by such "internal" names, but UiBinder * does not like when it sees unknown attributes, so we should remove them. */// ww w . ja v a 2 s.c om private static String removeWbpNameAttributes(String content) { Matcher matcher = Pattern.compile("wbp:name=\"[^\"]*\"").matcher(content); // process each match int last = 0; StringBuffer sb = new StringBuffer(); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); // not matched part sb.append(content.substring(last, start)); last = end; // replace matched part with spaces for (int i = start; i < end; i++) { sb.append(' '); } } // append tail sb.append(content.substring(last)); return sb.toString(); }
From source file:com.revolsys.util.JexlUtil.java
/** * <p>// w ww .j a v a 2s . c o m * Convert expressions into valid JexlExpressions, if the string does not * contain any expressions that match the expressionPattern then null will be * returned and the caller can use the raw string. * </p> * <p> * The expressionPattern can be used to define an alternate pattern than the * {@link #DEFAULT_EXPRESSION_PATTERN} that defines expressions in the form * ${el}. The pattern is defined as a Java Regular expression. The contents of * the expression part of the pattern must be enclosed in () to define the * group. The characters outside the first group will be removed from the * string and the expression portion will be added to the expression. * </p> * * @param expression The string containing expressions. * @param expressionPattern The regular expression pattern used to identify * expressions in the string. The first group in the expression will * be used as the expression. * @return The expression object for the string expression. * @throws Exception If there was an error creating the expression. */ public static Expression newExpression(final String expression, final String expressionPattern) throws Exception { final String newExpression = expression.replaceAll("\n", ""); // Wrap the entires expression in '' and replace the expressions in the // form "${expr)" to ' + expr + ' final Pattern compiledPattern = Pattern.compile(expressionPattern); final Matcher matcher = compiledPattern.matcher(newExpression); int lastEnd = 0; if (matcher.find()) { final StringBuilder jexlExpression = new StringBuilder(); do { final int startIndex = matcher.start(); if (startIndex != lastEnd) { final String text = newExpression.substring(lastEnd, startIndex); addText(jexlExpression, text); jexlExpression.append(" + "); } final String matchedExpression = matcher.group(1); jexlExpression.append(matchedExpression).append(" + "); lastEnd = matcher.end(); } while (matcher.find()); addText(jexlExpression, newExpression.substring(lastEnd)); // Remove any empty strings from the expression to improve // performance String expr = jexlExpression.toString(); expr = expr.replaceAll(" \\+ '' \\+ ", " + "); expr = expr.replaceAll("^'' \\+ ", ""); expr = expr.replaceAll("\\+ ''$", ""); return ExpressionFactory.createExpression(expr); } else { return null; } }
From source file:com.centurylink.mdw.util.ExpressionUtil.java
/** * Input is email template with image tags: * <code>/*w w w. ja va2s . c om*/ * <img src="${image:com.centurylink.mdw.base/mdw.png}" alt="MDW"> * </code> * Uses the unqualified image name as its CID. Populates imageMap with results. */ public static String substituteImages(String input, Map<String, String> imageMap) { StringBuffer substituted = new StringBuffer(input.length()); Matcher matcher = tokenPattern.matcher(input); int index = 0; while (matcher.find()) { String match = matcher.group(); substituted.append(input.substring(index, matcher.start())); if (imageMap != null && (match.startsWith("${image:"))) { String imageFile = match.substring(8, match.length() - 1); String imageId = imageFile.substring(imageFile.lastIndexOf('/') + 1); substituted.append("cid:" + imageId); imageMap.put(imageId, imageFile); } else { // ignore everything but images substituted.append(match); } index = matcher.end(); } substituted.append(input.substring(index)); return substituted.toString(); }
From source file:jp.co.ctc_g.jfw.core.util.Beans.java
/** * ?????????????//from w ww . ja v a 2s . co m * ???<code>getFoo</code><code>foo</code>? * <code>isBar</code><code>bar</code>?<code>setFoo</code><code>foo</code>??????? * @param accessorName ??????? * @return ?? */ public static String generatePropertyNameFor(String accessorName) { if (Strings.isEmpty(accessorName)) return accessorName; if (Beans.isPropertyAccessor(accessorName)) { Matcher matcher = FIRST_UPPERCASE_PATTERN.matcher(accessorName); return matcher.lookingAt() ? Beans.decapitalize(accessorName.substring(matcher.end() - 1)) : accessorName; } else { return accessorName; } }