List of usage examples for java.util.regex Matcher start
public int start()
From source file:com.github.gekoh.yagen.hibernate.PatchHibernateMappingClasses.java
private static int indexOfSeparator(String sql, int startIdx) { Matcher matcher = SEPARATOR_PATTERN.matcher(sql); if (matcher.find(startIdx)) { return matcher.start(); }/*from w w w . j a v a 2 s.co m*/ return -1; }
From source file:org.yamj.core.service.mediaimport.FilenameScanner.java
private static String cutMatch(String rest, Matcher matcher, String divider) { return rest.substring(0, matcher.start()) + divider + rest.substring(matcher.end()); }
From source file:com.kenshoo.freemarker.util.DataModelParser.java
public static Map<String, Object> parse(String src, TimeZone timeZone) throws DataModelParsingException { if (!StringUtils.hasText(src)) { return Collections.emptyMap(); }/*from ww w . j a v a2 s.co m*/ Map<String, Object> dataModel = new LinkedHashMap<>(); String lastName = null; int lastAssignmentStartEnd = 0; final Matcher assignmentStart = ASSIGNMENT_START.matcher(src); findAssignments: while (true) { boolean hasNextAssignment = assignmentStart.find(lastAssignmentStartEnd); if (lastName != null) { String value = src.substring(lastAssignmentStartEnd, hasNextAssignment ? assignmentStart.start() : src.length()).trim(); final Object parsedValue; try { parsedValue = parseValue(value, timeZone); } catch (DataModelParsingException e) { throw new DataModelParsingException( "Failed to parse the value of \"" + lastName + "\":\n" + e.getMessage(), e.getCause()); } dataModel.put(lastName, parsedValue); } if (lastName == null && (!hasNextAssignment || assignmentStart.start() != 0)) { throw new DataModelParsingException( "The data model specification must start with an assignment (name=value)."); } if (!hasNextAssignment) { break findAssignments; } lastName = assignmentStart.group(1).trim(); lastAssignmentStartEnd = assignmentStart.end(); } return dataModel; }
From source file:com.zimbra.common.util.TemplateCompiler.java
private static void convertLines(PrintWriter out, String pkg, String lines, Map<String, String> attrs, boolean authoritative) { out.print("AjxTemplate.register(\""); out.print(pkg);/*from ww w. j av a 2s. co m*/ out.println("\", "); out.println("function(name, params, data, buffer) {"); out.println("\tvar _hasBuffer = Boolean(buffer);"); out.println("\tdata = (typeof data == \"string\" ? { id: data } : data) || {};"); out.println("\tbuffer = buffer || [];"); out.println("\tvar _i = buffer.length;"); out.println(); Matcher matcher = RE_REPLACE.matcher(lines); if (matcher.find()) { int offset = 0; do { int index = matcher.start(); if (offset < index) { printStringLines(out, lines.substring(offset, index)); } String param = matcher.group(1); String inline = matcher.group(2); if (param != null) { printDataLine(out, param); } else if (inline != null) { printBufferLine(out, inline); } else { printLine(out, "\t", matcher.group(3).replaceAll("\n", "\n\t"), "\n"); } offset = matcher.end(); } while (matcher.find()); if (offset < lines.length()) { printStringLines(out, lines.substring(offset)); } } else { printStringLines(out, lines); } out.println(); out.println("\treturn _hasBuffer ? buffer.length : buffer.join(\"\");"); out.println("},"); if (attrs != null && attrs.size() > 0) { out.println("{"); Iterator<String> iter = attrs.keySet().iterator(); while (iter.hasNext()) { String aname = iter.next(); String avalue = attrs.get(aname); out.print("\t\""); printEscaped(out, aname); out.print("\": \""); printEscaped(out, avalue); out.print("\""); if (iter.hasNext()) { out.print(","); } out.println(); } out.print("}"); } else { out.print("null"); } out.print(", "); out.print(authoritative); out.println(");"); }
From source file:com.github.gekoh.yagen.hibernate.PatchHibernateMappingClasses.java
public static boolean isEmptyStatement(String sqlStmt) { Matcher matcher = COMMENT_PATTERN.matcher(sqlStmt); while (matcher.find()) { sqlStmt = sqlStmt.substring(0, matcher.start()) + sqlStmt.substring(matcher.end()); matcher = COMMENT_PATTERN.matcher(sqlStmt); }/*w w w. j a v a 2 s .c o m*/ return sqlStmt.trim().length() < 1; }
From source file:com.fsck.k9.helper.Utility.java
public static String extractMessageId(final String text) { Matcher matcher = MESSAGE_ID.matcher(text); if (matcher.find()) { return text.substring(matcher.start(), matcher.end()); }/*w ww . j a v a2 s . co m*/ return null; }
From source file:Main.java
public static String unescapeStringForXML(String s) { StringBuilder result = new StringBuilder(); Matcher m = xmlEscapingPattern.matcher(s); int end = 0;/* w ww . j a va2 s. co m*/ while (m.find()) { int start = m.start(); result.append(s.substring(end, start)); end = m.end(); result.append(translate(s.substring(start, end))); } result.append(s.substring(end, s.length())); return result.toString(); }
From source file:de.fau.cs.osr.utils.StringUtils.java
public static String xmlDecode(String text, XmlEntityResolver resolver) { Pattern rx = XmlGrammar.xmlReference(); int start = 0; StringBuilder b = new StringBuilder(); while (true) { Matcher m = rx.matcher(text); if (m.find(start)) { b.append(text.substring(start, m.start())); String resolved = null; if (m.group(1) != null) { resolved = resolver.resolveXmlEntity(m.group(1)); } else { try { boolean decimal = m.group(2) != null; String num = decimal ? m.group(2) : m.group(3); int val = Integer.valueOf(num, decimal ? 10 : 16); if (val >= 0x20 && val != 0x7F) resolved = String.valueOf((char) val); } catch (NumberFormatException e) { }// www .ja va 2 s. c o m } if (resolved != null) b.append(resolved); else b.append(text.substring(m.start(), m.end())); start = m.end(); } else { if (start < text.length()) b.append(text.substring(start)); break; } } return b.toString(); }
From source file:msi.gama.util.GAML.java
/** * Version of lastIndexOf that uses regular expressions for searching. By Tomer Godinger. * //from w ww .j av a 2 s . co m * @param str * String in which to search for the pattern. * @param toFind * Pattern to locate. * @return The index of the requested pattern, if found; NOT_FOUND (-1) otherwise. */ public static int lastIndexOfRegex(final String str, final String toFind) { final Pattern pattern = Pattern.compile(toFind); final Matcher matcher = pattern.matcher(str); // Default to the NOT_FOUND constant int lastIndex = NOT_FOUND; // Search for the given pattern while (matcher.find()) { lastIndex = matcher.start(); } return lastIndex; }
From source file:com.android.dialer.lookup.whitepages.WhitePagesApi.java
private static ContactInfo[] parseOutputUnitedStates(String output, int maxResults) throws IOException { ArrayList<ContactInfo> people = new ArrayList<ContactInfo>(); Pattern regex = Pattern.compile("<li\\s[^>]+?http:\\/\\/schema\\.org\\/Person", Pattern.DOTALL); Matcher m = regex.matcher(output); while (m.find()) { if (people.size() == maxResults) { break; }/* w w w . j a v a 2 s. c om*/ // Find section of HTML with contact information String section = extractXmlTag(output, m.start(), m.end(), "li"); // Skip entries with no phone number if (section.contains("has-no-phone-icon")) { continue; } String name = LookupUtils.fromHtml(extractXmlRegex(section, "<span[^>]+?itemprop=\"name\">", "span")); if (name == null) { continue; } // Address String addrCountry = LookupUtils .fromHtml(extractXmlRegex(section, "<span[^>]+?itemprop=\"addressCountry\">", "span")); String addrState = LookupUtils .fromHtml(extractXmlRegex(section, "<span[^>]+?itemprop=\"addressRegion\">", "span")); String addrCity = LookupUtils .fromHtml(extractXmlRegex(section, "<span[^>]+?itemprop=\"addressLocality\">", "span")); StringBuilder sb = new StringBuilder(); if (addrCity != null) { sb.append(addrCity); } if (addrState != null) { if (sb.length() > 0) { sb.append(", "); } sb.append(addrState); } if (addrCountry != null) { if (sb.length() > 0) { sb.append(", "); } sb.append(addrCountry); } // Website Pattern p = Pattern.compile("href=\"(.+?)\""); Matcher m2 = p.matcher(section); String website = null; if (m2.find()) { website = "http://www.whitepages.com" + m2.group(1); } // Phone number is on profile page, so skip if we can't get the // website if (website == null) { continue; } String profile = httpGet(website); String phoneNumber = LookupUtils .fromHtml(extractXmlRegex(profile, "<li[^>]+?class=\"no-overflow tel\">", "li")); String address = parseAddressUnitedStates(profile); if (phoneNumber == null) { Log.e(TAG, "Phone number is null. Either cookie is bad or regex is broken"); continue; } ContactInfo info = new ContactInfo(); info.name = name; info.city = sb.toString(); info.address = address; info.formattedNumber = phoneNumber; info.website = website; people.add(info); } return people.toArray(new ContactInfo[people.size()]); }