List of usage examples for java.util.regex Matcher group
public String group()
From source file:Main.java
public static String getHost(String url) { if (url == null || "".equals(url.trim())) return null; Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+"); Matcher matcher = p.matcher(url); if (matcher.find()) return matcher.group(); else/*from www . j a va 2 s . c om*/ return null; }
From source file:com.plan.proyecto.servicios.utilidades.UrlParser.java
public static List<String> pullLinks(String text) { List<String> links = new ArrayList(); String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(text); while (m.find()) { String urlStr = m.group(); if (urlStr.startsWith("(") && urlStr.endsWith(")")) { urlStr = urlStr.substring(1, urlStr.length() - 1); }//from ww w .j a v a 2 s . c o m links.add(urlStr); } return links; }
From source file:Main.java
/** * Extracts ID from the video url/*www. j a va2 s . c om*/ * @param youtubeUrl the video url * @return the ID of the video */ public static String extractYouTubeVideoId(String youtubeUrl) { String pattern = "(?<=youtu.be/|watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*"; Pattern compiledPattern = Pattern.compile(pattern); Matcher matcher = compiledPattern.matcher(youtubeUrl); if (matcher.find()) { return matcher.group(); } else { return "H7B1NhceHEg"; } }
From source file:Main.java
private static String getFileNameByUrl(String url) { // TODO Auto-generated method stub Pattern p = Pattern.compile("\\w+"); Matcher m = p.matcher(url); String filename = ""; while (m.find()) { String str = m.group(); filename = str;/* w ww . j ava2 s. c o m*/ } return filename; }
From source file:Main.java
public static ArrayList<String> retrieveLinks(String text) { ArrayList<String> links = new ArrayList<String>(); String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(text); while (m.find()) { String urlStr = m.group(); if (urlStr.startsWith("(") && urlStr.endsWith(")")) { char[] stringArray = urlStr.toCharArray(); char[] newArray = new char[stringArray.length - 2]; System.arraycopy(stringArray, 1, newArray, 0, stringArray.length - 2); urlStr = new String(newArray); System.out.println("Finally Url =" + newArray.toString()); }/*from w ww.j a v a2 s . c o m*/ System.out.println("...Url..." + urlStr); links.add(urlStr); } return links; }
From source file:Main.java
public static String getConditionOfExpression(String expression) { // pattern for conditions Pattern conditionPattern = Pattern .compile("^([\\w\\W&&[^/\\[\\]]]+)?(\\[[\\w\\W&&[^\\[\\]]]+\\])(/[\\w\\W]+)?$"); Matcher conditionMatcher = conditionPattern.matcher(expression); if (conditionMatcher.find() && (conditionMatcher.group().length() == expression.length())) { StringTokenizer st = new StringTokenizer(expression, "]"); String condition = st.nextToken(); condition = condition.substring(condition.indexOf("[") + 1); return condition; }//from www. j a v a2 s .c o m return null; }
From source file:Main.java
public static SpannableString StringToBitMap(Context context, String msg) { SpannableString ss = new SpannableString(msg); Pattern p = Pattern.compile("/mnt/sdcard/.+?\\.\\w{3}"); Matcher m = p.matcher(msg); while (m.find()) { Bitmap bitmap = BitmapFactory.decodeFile(m.group()); ImageSpan imageSpan = new ImageSpan(context, bitmap); ss.setSpan(imageSpan, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }/*from w w w . jav a 2 s.co m*/ return ss; }
From source file:Main.java
public static String matchCamelCase(String query, String str) { if (!query.matches("[A-Za-z\\*]+")) { return null; }/*from w ww . j a v a 2 s. co m*/ String head = ""; int i; for (i = 0; i < query.length(); i++) { char charI = query.charAt(i); if (Character.isLowerCase(charI)) { head += charI; } else { break; } } if (i > 0) { head += "[^A-Z]*"; } String tail = query.substring(i); String re = "\\b("; tail = tail.replaceAll("\\*", ".*?"); re += head + tail.replaceAll("([A-Z][^A-Z]*)", "$1[^A-Z]*"); re += ".*?)\\b"; Pattern regex = Pattern.compile(re); Matcher m = regex.matcher(str); if (m.find()) { return m.group(); } else { return null; } }
From source file:Main.java
public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern, Drawable drawable) {//ww w. j a v a 2 s. c om SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence : new SpannableString(charSequence); try { ImageSpan is = new ImageSpan(drawable); Pattern pattern = Pattern.compile(regPattern); Matcher matcher = pattern.matcher(ss); while (matcher.find()) { String key = matcher.group(); ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } } catch (Exception ex) { } return ss; }
From source file:Main.java
public static List<String> findAllMatches(String regularExpression, String contentsString) { List<String> allMatches = new ArrayList<String>(); Matcher m = Pattern.compile(regularExpression).matcher(contentsString); while (m.find()) { allMatches.add(m.group()); }/* www . j a v a 2 s . c o m*/ return allMatches; }