List of usage examples for java.util.regex Matcher start
public int start()
From source file:com.pfarrell.utils.misc.TextTools.java
/** * unquote a string surrounded by single or double quote possible with trailing punctuation * @param arg input string/* w ww . j ava 2 s .c o m*/ * @return unquoted string */ public static String unquoteWithTrailing(String arg) { String rval = arg; if (arg != null && arg.length() > 2) { char c = arg.charAt(0); Pattern trailingPattern = Pattern.compile("(\\" + c + ")[,.]*$"); Matcher m = trailingPattern.matcher(arg); if (m.find()) { rval = arg.substring(1, m.start()).trim(); } } return rval; }
From source file:com.github.thorqin.toolkit.mail.MailService.java
public static Mail createMailByTemplateStream(InputStream in, Map<String, String> replaced) throws IOException { Mail mail = new Mail(); InputStreamReader reader = new InputStreamReader(in, "utf-8"); char[] buffer = new char[1024]; StringBuilder builder = new StringBuilder(); while (reader.read(buffer) != -1) builder.append(buffer);//www .j a v a2s .co m String mailBody = builder.toString(); builder.setLength(0); Pattern pattern = Pattern.compile("<%\\s*(.+?)\\s*%>", Pattern.MULTILINE); Matcher matcher = pattern.matcher(mailBody); int scanPos = 0; while (matcher.find()) { builder.append(mailBody.substring(scanPos, matcher.start())); scanPos = matcher.end(); String key = matcher.group(1); if (replaced != null) { String value = replaced.get(key); if (value != null) { builder.append(value); } } } builder.append(mailBody.substring(scanPos, mailBody.length())); mail.htmlBody = builder.toString(); pattern = Pattern.compile("<title>(.*)</title>", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE); matcher = pattern.matcher(mail.htmlBody); if (matcher.find()) { mail.subject = matcher.group(1); } return mail; }
From source file:com.github.thorqin.webapi.mail.MailService.java
public static Mail createHtmlMailFromTemplate(String templatePath, Map<String, String> replaced) { Mail mail = new Mail(); try (InputStream in = MailService.class.getClassLoader().getResourceAsStream(templatePath)) { InputStreamReader reader = new InputStreamReader(in, "utf-8"); char[] buffer = new char[1024]; StringBuilder builder = new StringBuilder(); while (reader.read(buffer) != -1) builder.append(buffer);//from w w w .java 2 s . c o m String mailBody = builder.toString(); builder.setLength(0); Pattern pattern = Pattern.compile("<%\\s*(.+?)\\s*%>", Pattern.MULTILINE); Matcher matcher = pattern.matcher(mailBody); int scanPos = 0; while (matcher.find()) { builder.append(mailBody.substring(scanPos, matcher.start())); scanPos = matcher.end(); String key = matcher.group(1); if (replaced != null) { String value = replaced.get(key); if (value != null) { builder.append(value); } } } builder.append(mailBody.substring(scanPos, mailBody.length())); mail.htmlBody = builder.toString(); pattern = Pattern.compile("<title>(.*)</title>", Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE); matcher = pattern.matcher(mail.htmlBody); if (matcher.find()) { mail.subject = matcher.group(1); } } catch (IOException ex) { logger.log(Level.SEVERE, "Create mail from template error: {0}, {1}", new Object[] { templatePath, ex }); } return mail; }
From source file:edu.uci.ics.asterix.result.ResultUtils.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("SyntaxError: " + 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); }/*w w w. j a va2 s . c om*/ } } return errorMessage.toString(); }
From source file:StringUtils.java
public static final List<String> explode(String string, Pattern separator) { int lastIndex = 0, len = string.length(); Matcher matcher = separator.matcher(string); List<String> ret = new LinkedList<String>(); while (matcher.find()) { String s = string.substring(lastIndex, matcher.start()); if (s.length() > 0) ret.add(s);/* www .j a v a2 s . co m*/ lastIndex = matcher.end(); } String s = string.substring(lastIndex, len); if (s.length() > 0) ret.add(s); return ret; }
From source file:Main.java
public static CharSequence replace(Context context, String text) { if (TextUtils.isEmpty(text)) { return text; }//from w ww .j a v a 2 s .c o m SpannableString spannableString = new SpannableString(text); Matcher matcher = pattern.matcher(text); while (matcher.find()) { String factText = matcher.group(); String key = factText.substring(1, factText.length() - 1); if (contain(emojiCodes, factText)) { Bitmap bitmap = getEmojiDrawable(context, key); ImageSpan image = new ImageSpan(context, bitmap); int start = matcher.start(); int end = matcher.end(); spannableString.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } return spannableString; }
From source file:net.sf.xmm.moviemanager.http.HttpUtil.java
public static StringBuffer getHtmlNiceFormat(StringBuffer buffer) { int index = 0; // Format html Pattern p = Pattern.compile("</.+?>"); Matcher m = p.matcher(buffer); while (m.find(index)) { index = m.start(); int index2 = buffer.indexOf(">", index) + 1; buffer.insert(index2, SysUtil.getLineSeparator()); index++;//from w w w . ja v a 2 s. c o m } return buffer; }
From source file:wuit.common.crawler.WebSit.Crawler.java
public static void matchValues(String content, String filter, List<KeyValue> list) { if (list == null) list = new ArrayList<KeyValue>(); try {//from ww w .j a v a2s. c o m Matcher m = Pattern.compile(filter, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE).matcher(content); while (m.find()) { if (m.group().isEmpty()) continue; KeyValue value = new KeyValue(); value.value = m.group(); value.start = m.start(); value.end = m.end(); list.add(value); } } catch (Exception e) { System.out.println("Crawler Utitles matchValues :" + e.getMessage()); } }
From source file:Main.java
public static CharSequence handleAcUrl(CharSequence content) { Matcher m = AC_PATTERN.matcher(content); Spannable spannable = null;//from w w w .ja v a 2 s . c o m while (m.find()) { // Ensure spannable if (spannable == null) { if (content instanceof Spannable) { spannable = (Spannable) content; } else { spannable = new SpannableString(content); } } int start = m.start(); int end = m.end(); URLSpan[] links = spannable.getSpans(start, end, URLSpan.class); if (links.length > 0) { // There has been URLSpan already, leave it alone continue; } URLSpan urlSpan = new URLSpan("http://www.acfun.tv/v/" + m.group(0)); spannable.setSpan(urlSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } return spannable == null ? content : spannable; }
From source file:Main.java
public static String toHref(String title) { StringBuffer sb = new StringBuffer(title); Pattern pat = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Matcher mat = pat.matcher(title); int index = 0; int index1 = 0; while (mat.find()) { String url = mat.group(); //System.out.println(url); if (url.indexOf("http://") != 0) url = "http://" + url; Object obj[] = { "'" + url + "'" }; String a = MessageFormat.format(A1, obj); int l = a.length(); index += index1;//ww w .ja v a 2 s . co m sb.insert(mat.start() + index, a); index += l; sb.insert((mat.end()) + index, A2); index1 = A2.length(); } return sb.toString(); }