List of usage examples for java.util.regex Matcher find
public boolean find()
From source file:Main.java
public static StringBuffer removePatternContents(String defStr, Pattern pat) { Matcher mat = pat.matcher(defStr); StringBuffer sb = new StringBuffer(); while (mat.find()) { mat.appendReplacement(sb, " "); }//ww w.j a v a 2 s . co m mat.appendTail(sb); return sb; }
From source file:Main.java
/** * Takes the characters until the pattern is matched *//*from ww w . j a v a2 s . c o m*/ public static String takeUntil(Object src, Object pattern) { String src2 = src.toString(); Matcher matcher = Pattern.compile(pattern.toString()).matcher(src2); if (!matcher.find()) return src2; int index = matcher.start(); if (index == -1) { return src2; } return src2.substring(0, index); }
From source file:Main.java
public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern, Drawable drawable) {/* ww w . ja v a 2s . c o m*/ 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) { ex.printStackTrace(); } return ss; }
From source file:Main.java
public static List<String> extractUrl(String text) { List<String> urls = new ArrayList<>(); Matcher matcher = Patterns.WEB_URL.matcher(text); while (matcher.find()) { int start = matcher.start(); if (start > 0 && text.charAt(start - 1) == '@') { continue; }//from w w w . j a v a2s . c om String url = matcher.group(); if (!url.startsWith("http")) { url = "http://" + url; } urls.add(url); } return urls; }
From source file:Main.java
public static List<int[]> getLinksRanges(String s) { List<int[]> ranges = new ArrayList<int[]>(); Matcher matcher = WEB_URL_PATTERN.matcher(s); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); ranges.add(new int[] { start, end }); }//from w ww . ja v a 2 s . c o m return ranges; }
From source file:Main.java
/** * Replaces a tag given by a tagname in an xmlstring. * //from ww w .ja va 2s . com * @param tagname Name of the tag to be removed * @param xmlstring XmlString to be modified * @return modified XmlString */ public static String removeTag(String tagname, String xmlstring) { if (!patterns.containsKey(tagname)) { String regex = "(\\<" + tagname + ">.+?\\</" + tagname + ">)"; Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | Pattern.MULTILINE); patterns.put(tagname, pattern); } Matcher matcher = patterns.get(tagname).matcher(xmlstring); if (matcher.find()) { xmlstring = matcher.replaceAll(""); } return xmlstring; }
From source file:Correct.java
public static void execute(String One, String Two) { Two = Two.replaceAll("\\\\n", "\n"); try {/* ww w .jav a 2 s . c o m*/ System.out.println("Regex = " + One); System.out.println("Input = " + Two); Pattern pattern = Pattern.compile(One); Matcher match = pattern.matcher(Two); while (match.find()) { System.out.println("Found [" + match.group() + "]\nStarting at " + match.start() + " , \nEnding at " + (match.end() - 1)); } } catch (PatternSyntaxException pse) { System.err.println("Bad regex: " + pse.getMessage()); System.err.println("Description: " + pse.getDescription()); System.err.println("Index: " + pse.getIndex()); System.err.println("Incorrect pattern: " + pse.getPattern()); } }
From source file:Main.java
public static String getClassNameFromTableName(String tableName) { Pattern p = Pattern.compile("(_+|^)(\\w?)"); Matcher m = p.matcher(tableName); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, m.group(2).toUpperCase()); }/*from www. j a v a2 s . c o m*/ m.appendTail(sb); return sb.toString(); }
From source file:Main.java
/** * Get all the matches for {@code string} compiled by {@code pattern}. If * {@code isGlobal} is true, the return results will only include the * group 0 matches. It is similar to string.match(regexp) in JavaScript. * //from w w w. ja v a2s . co m * @param pattern the regexp * @param string the string * @param isGlobal similar to JavaScript /g flag * * @return all matches */ public static String[] match(Pattern pattern, String string, boolean isGlobal) { if (pattern == null) { throw new NullPointerException("argument 'pattern' cannot be null"); } if (string == null) { throw new NullPointerException("argument 'string' cannot be null"); } List<String> matchesList = new ArrayList<String>(); Matcher matcher = pattern.matcher(string); while (matcher.find()) { matchesList.add(matcher.group(0)); if (!isGlobal) { for (int i = 1, iEnd = matcher.groupCount(); i <= iEnd; i++) { matchesList.add(matcher.group(i)); } } } return matchesList.toArray(new String[matchesList.size()]); }
From source file:Main.java
/** * Splits the command string around blank spaces avoiding strings surrounded by quotation marks. * @param vboxManageCmdLine the command to be handled * @return a list of strings of the split command string pieces *///from w ww . j a v a2 s. c o m public static List<String> splitCmdLine(String vboxManageCmdLine) { List<String> matchList = new ArrayList<String>(); Pattern regex = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'"); Matcher regexMatcher = regex.matcher(vboxManageCmdLine); while (regexMatcher.find()) { if (regexMatcher.group(1) != null) { // Add double-quoted string without the quotes matchList.add(regexMatcher.group(1)); } else if (regexMatcher.group(2) != null) { // Add single-quoted string without the quotes matchList.add(regexMatcher.group(2)); } else { // Add unquoted word matchList.add(regexMatcher.group()); } } return matchList; }