List of usage examples for java.util.regex Matcher start
public int start()
From source file:Main.java
public static CharSequence replace(Context ctx, String text) { SpannableString spannableString = new SpannableString(text); Matcher matcher = pattern.matcher(text); while (matcher.find()) { String factText = matcher.group(); String key = factText.substring(1); if (emotionTexts.contains(factText)) { Bitmap bitmap = getDrawableByName(ctx, key); ImageSpan image = new ImageSpan(ctx, bitmap); int start = matcher.start(); int end = matcher.end(); spannableString.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }// w w w . j av a 2 s .co m } return spannableString; }
From source file:Main.java
public static CharSequence replace(Context ctx, String text) { if (text == null) { return null; }/*from w ww . j av a2s. c om*/ SpannableString spannableString = new SpannableString(text); Matcher matcher = pattern.matcher(text); while (matcher.find()) { String factText = matcher.group(); String key = factText.substring(1); if (emotionTexts.contains(factText)) { Bitmap bitmap = getDrawableByName(ctx, key); ImageSpan image = new ImageSpan(ctx, bitmap); int start = matcher.start(); int end = matcher.end(); spannableString.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } return spannableString; }
From source file:com.android.dialer.lookup.whitepages.WhitePagesApi.java
private static String extractXmlRegex(String str, String regex, String tag) { Pattern p = Pattern.compile(regex, Pattern.DOTALL); Matcher m = p.matcher(str); if (m.find()) { return extractXmlTag(str, m.start(), m.end(), tag); }/*from w ww . ja v a 2 s.c o m*/ return null; }
From source file:eu.eubrazilcc.lvl.core.util.QueryUtils.java
public static ImmutableMap<String, String> parseQuery(final String query, final boolean deduplicate) { final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); if (isNotBlank(query)) { String fullText = query;//from w ww . j av a 2 s. c om final Matcher matcher = KEYWORD_PATTERN.matcher(query); while (matcher.find()) { final String[] keyword = extractKeyword(matcher.group()); builder.put(keyword[0], remove(keyword[1], QUOTES)); fullText = strikethrough(fullText, matcher.start(), matcher.end()); } fullText = deduplicate ? normalize(fullText) : normalizeSpace(fullText); if (isNotBlank(fullText)) { builder.put(TEXT_FIELD, remove(fullText, QUOTES)); } } return builder.build(); }
From source file:gr.scify.newsum.Utils.java
/** * returns the substring of the given string from 0 to pattern match start * @param sText the given text/* ww w. j a va 2 s.co m*/ * @param sRegex the matcher * @return the substring of the given string from 0 to pattern match start */ public static String removeSourcesParen(String sText) { String sRegex = "\\(\\d+\\)\\s*\\Z"; Matcher m = Pattern.compile(sRegex).matcher(sText); if (m.find()) { return sText.substring(0, m.start()); } return sText; }
From source file:Main.java
public static void makeAboutSpannable(SpannableStringBuilder span, String str_link, String replace, final Runnable on_click) { Pattern pattern = Pattern.compile(str_link); Matcher matcher = pattern.matcher(span); ForegroundColorSpan color_theme = new ForegroundColorSpan(Color.parseColor("#53b7bb")); if (matcher.find()) { span.setSpan(new ClickableSpan() { @Override/*from w ww . j av a 2 s .c om*/ public void onClick(View widget) { if (on_click != null) on_click.run(); } }, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); span.setSpan(color_theme, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); if (replace != null) span.replace(matcher.start(), matcher.end(), replace); } }
From source file:com.microsoft.tfs.util.PlatformVersion.java
/** * Returns the integer components of the given version number string. * "Components" are defined as any non-alphanumeric separated string of * alphanumeric characters. Only the integer components are returned and * only up to a non-integer component. For example, given the * "10_4.21Q.Z.14", the return will be [ 10, 4, 21 ]. * * @param versionString/* w w w .j a v a 2s . c o m*/ * The version number string * @return An integer array of the numeric components of the version number * string */ static final int[] parseVersionNumber(final String versionString) { if (versionString == null || versionString.length() == 0) { return new int[0]; } /* * Split into component pieces -- everything that is non-alphanumeric is * considered a version number separator. */ final String[] stringComponents = versionString.split("[^a-zA-Z0-9]"); //$NON-NLS-1$ final ArrayList versionComponents = new ArrayList(); /* * We trim components to keep only the leading numeric value. */ synchronized (PlatformVersion.class) { if (componentPattern == null) { try { componentPattern = Pattern.compile("^([0-9]+)"); //$NON-NLS-1$ } catch (final Exception e) { logger.error("Could not compile version number regex", e); //$NON-NLS-1$ return new int[0]; } } } for (int i = 0; i < stringComponents.length; i++) { /* * Trim to keep only the leading numeric value. If we're left with * nothing (ie, this component started with a letter), then do not * add it to the version number and stop. */ final Matcher match = componentPattern.matcher(stringComponents[i]); if (match.find()) { try { versionComponents.add(new Integer(stringComponents[i].substring(match.start(), match.end()))); } catch (final Exception e) { logger.warn("Could not coerce version number into format: " + versionString, e); //$NON-NLS-1$ break; } } else { /* Component did not start with a numeric value, stopping. */ break; } } /* Strip trailing zeroes. */ while (versionComponents.size() > 0 && versionComponents.get(versionComponents.size() - 1).equals(INTEGER_ZERO)) { versionComponents.remove(versionComponents.size() - 1); } /* Convert to int array */ final int[] version = new int[versionComponents.size()]; for (int i = 0; i < versionComponents.size(); i++) { version[i] = ((Integer) versionComponents.get(i)).intValue(); } return version; }
From source file:com.screenslicer.common.HtmlCoder.java
private static String decode(String string, boolean isAttributeValue) { try {//from w ww.j ava2 s . c o m if (CommonUtil.isEmpty(string)) { return ""; } Matcher matcher = regexDecode.matcher(string); StringBuilder builder = new StringBuilder(); int prevEnd = 0; while (matcher.find()) { builder.append(string.substring(prevEnd, matcher.start())); prevEnd = matcher.end(); int codePoint; String hexDigits; String reference; String next; if (!CommonUtil.isEmpty(matcher.group(1))) { // Decode decimal escapes, e.g. `𝌆`. codePoint = Integer.parseInt(matcher.group(1)); builder.append(codePointToSymbol(codePoint)); continue; } if (!CommonUtil.isEmpty(matcher.group(3))) { // Decode hexadecimal escapes, e.g. `𝌆`. hexDigits = matcher.group(3); codePoint = Integer.parseInt(hexDigits, 16); builder.append(codePointToSymbol(codePoint)); continue; } if (!CommonUtil.isEmpty(matcher.group(5))) { // Decode named character references with trailing `;`, e.g. `©`. reference = matcher.group(5); if (decodeMap.containsKey(reference)) { builder.append(decodeMap.get(reference)); continue; } else { // Ambiguous ampersand. https://mths.be/notes/ambiguous-ampersands builder.append(matcher.group(0)); continue; } } // If were still here, its a legacy reference for sure. No need for an // extra `if` check. // Decode named character references without trailing `;`, e.g. `&` // This is only a parse error if it gets converted to `&`, or if it is // followed by `=` in an attribute context. reference = matcher.group(6); next = matcher.group(7); if (!CommonUtil.isEmpty(next) && isAttributeValue) { builder.append(matcher.group(0)); continue; } else if (decodeMapLegacy.containsKey(reference)) { builder.append(decodeMapLegacy.get(reference) + (CommonUtil.isEmpty(next) ? "" : next)); continue; } } builder.append(string.substring(prevEnd, string.length())); return builder.toString(); } catch (Throwable t) { Log.exception(t); return string; } }
From source file:de.hasait.genesis.base.util.GenesisUtils.java
public static String camelCaseToUpperUnderscore(final String pInput) { if (StringUtils.isEmpty(pInput)) { return pInput; }/*from w ww. ja v a 2 s. c om*/ final Pattern camelPattern = Pattern.compile("\\p{Lower}\\p{Upper}"); final Matcher camelMatcher = camelPattern.matcher(pInput); final StringBuilder result = new StringBuilder(); int current = 0; while (camelMatcher.find(current)) { final int split = camelMatcher.start() + 1; result.append(pInput.substring(current, split).toUpperCase()); result.append('_'); current = split; } result.append(pInput.substring(current).toUpperCase()); return result.toString(); }
From source file:org.apache.asterix.api.http.server.ResultUtil.java
public static String buildParseExceptionMessage(Throwable e, String query) { StringBuilder errorMessage = new StringBuilder(); String message = e.getMessage(); message = message.replace("<", "<"); message = message.replace(">", ">"); errorMessage.append("Error: " + message + "\n"); int pos = message.indexOf("line"); if (pos > 0) { Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(message); if (m.find(pos)) { int lineNo = Integer.parseInt(message.substring(m.start(), m.end())); String[] lines = query.split("\n"); if (lineNo > lines.length) { errorMessage.append("===> <BLANK LINE> \n"); } else { String line = lines[lineNo - 1]; errorMessage.append("==> " + line); }/*from ww w . j ava2 s . c o m*/ } } return errorMessage.toString(); }