List of usage examples for java.util.regex Matcher end
public int end(String name)
From source file:gov.nih.nci.cananolab.util.StringUtils.java
/** * Parse the text into an array of words using white space as delimiter. * Keeping words in quotes together./*from www . j a va 2s . co m*/ * * @param texts * @return */ public static List<String> parseToWords(String text) { if (isEmpty(text)) { return null; } SortedSet<String> wordList = new TreeSet<String>(); // extract words in quotes first String patternStr = "\\B[\"']([^\"']*)[\"']\\B"; String[] nonQuotedTexts = text.split(patternStr); for (String txt : nonQuotedTexts) { String[] nonQuotedWords = txt.split("\\s"); wordList.addAll(Arrays.asList(nonQuotedWords)); } Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(text); List<String> quotedWords = new ArrayList<String>(); int start = 0; while (matcher.find(start)) { String quotedWord = matcher.group(1).trim(); quotedWords.add(quotedWord); start = matcher.end(1); } wordList.addAll(quotedWords); return new ArrayList<String>(wordList); }
From source file:de.uniwue.info6.misc.StringTools.java
/** * * * @param queryText//from w w w . j a v a2 s . co m * @param tables * @param user * @return */ public static String addUserPrefix(String queryText, List<String> tables, User user) { LinkedList<Integer> substrings = new LinkedList<Integer>(); int start = 0, end = queryText.length(); for (String tab : tables) { // q&d fix Matcher matcher = Pattern.compile(tab.trim(), Pattern.CASE_INSENSITIVE).matcher(queryText); while (matcher.find()) { start = matcher.start(0); end = matcher.end(0); // String group = matcher.group(); boolean leftCharacterValid = StringTools.trailingCharacter(queryText, start, true); boolean rightCharacterValid = StringTools.trailingCharacter(queryText, end, false); if (leftCharacterValid && rightCharacterValid) { substrings.add(start); } } } Collections.sort(substrings); for (int i = substrings.size() - 1; i >= 0; i--) { Integer sub = substrings.get(i); queryText = queryText.substring(0, sub) + user.getId() + "_" + queryText.substring(sub, queryText.length()); } return queryText; }
From source file:com.github.gekoh.yagen.util.FieldInfo.java
private static String formatAnnotation(Annotation annotation) { String a = annotation.toString(); StringBuilder result = new StringBuilder(); // wrap string value of attribute "name" into double quotes as needed for java code Matcher m = STRING_ATTR_PATTERN.matcher(a); int idx = 0;/*from w w w. jav a 2 s . c om*/ while (m.find(idx)) { result.append(a.substring(idx, m.start(2))); result.append("\"").append(escapeAttributeValue(m.group(2))).append("\""); result.append(a.substring(m.end(2), m.end())); idx = m.end(); } result.append(a.substring(idx)); a = result.toString(); result = new StringBuilder(); // remove empty attributes like (columnDefinition=) m = Pattern.compile("\\(?(,?\\s*[A-Za-z]*=)[,|\\)]").matcher(a); idx = 0; while (m.find(idx)) { result.append(a.substring(idx, m.start(1))); idx = m.end(1); } result.append(a.substring(idx)); // set nullable=true m = NULLABLE_PATTERN.matcher(result); idx = 0; while (m.find(idx)) { if (m.group(1).equals("false")) { result.replace(m.start(1), m.end(1), "true"); } idx = m.start(1) + 1; m = NULLABLE_PATTERN.matcher(result); } // set unique=false m = UNIQUE_PATTERN.matcher(result); idx = 0; while (m.find(idx)) { if (m.group(1).equals("true")) { result.replace(m.start(1), m.end(1), "false"); } idx = m.start(1) + 1; m = UNIQUE_PATTERN.matcher(result); } return result.toString().replaceAll("=\\[([^\\]]*)\\]", "={$1}"); }
From source file:it.unibo.alchemist.language.protelis.util.ProtelisLoader.java
private static void loadResourcesRecursively(final XtextResourceSet target, final String programURI, final Set<String> alreadyInQueue) throws IOException { final String realURI = (programURI.startsWith("/") ? "classpath:" : "") + programURI; if (!alreadyInQueue.contains(realURI)) { alreadyInQueue.add(realURI);//from w ww . j a va 2 s . co m final URI uri = URI.createURI(realURI); final org.springframework.core.io.Resource protelisFile = RESOLVER.getResource(realURI); final InputStream is = protelisFile.getInputStream(); final String ss = IOUtils.toString(is, "UTF-8"); final Matcher matcher = REGEX_PROTELIS_IMPORT.matcher(ss); while (matcher.find()) { final int start = matcher.start(1); final int end = matcher.end(1); final String imp = ss.substring(start, end); final String classpathResource = "classpath:/" + imp.replace(":", "/") + "." + PROTELIS_FILE_EXTENSION; loadResourcesRecursively(target, classpathResource, alreadyInQueue); } target.getResource(uri, true); } }
From source file:org.protelis.lang.ProtelisLoader.java
private static void loadStringResources(final XtextResourceSet target, final InputStream is) throws IOException { final Set<String> alreadyInQueue = new LinkedHashSet<>(); final String ss = IOUtils.toString(is, "UTF-8"); final Matcher matcher = REGEX_PROTELIS_IMPORT.matcher(ss); while (matcher.find()) { final int start = matcher.start(1); final int end = matcher.end(1); final String imp = ss.substring(start, end); final String classpathResource = "classpath:/" + imp.replace(":", "/") + "." + PROTELIS_FILE_EXTENSION; loadResourcesRecursively(target, classpathResource, alreadyInQueue); }/*from w w w .j a v a 2 s .c o m*/ }
From source file:com.feilong.core.util.RegexUtil.java
/** * ??????./*from w ww.j a va 2s . co m*/ * * <p> * ? m?? s g,? m.group(g) s.substring(m.start(g), m.end(g)).<br> * ? 1?.0?,? m.group(0) m.group(). * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * String regexPattern = "(.*?)@(.*?)"; * String email = "venusdrogon@163.com"; * RegexUtil.group(regexPattern, email); * </pre> * * <b>:</b> * * <pre class="code"> * 0 venusdrogon@163.com * 1 venusdrogon * 2 163.com * </pre> * * </blockquote> * * @param regexPattern * ?,pls use {@link RegexPattern} * @param input * The character sequence to be matched,support {@link String},{@link StringBuffer},{@link StringBuilder}... and so on * @return <code>regexPattern</code> null, {@link NullPointerException}<br> * <code>input</code> null, {@link NullPointerException}<br> * ??, {@link java.util.Collections#emptyMap()} * @see #getMatcher(String, CharSequence) * @see Matcher#group(int) * @since 1.0.7 */ public static Map<Integer, String> group(String regexPattern, CharSequence input) { Matcher matcher = getMatcher(regexPattern, input); if (!matcher.matches()) { LOGGER.trace("[not matches] ,\n\tregexPattern:[{}] \n\tinput:[{}]", regexPattern, input); return Collections.emptyMap(); } int groupCount = matcher.groupCount(); Map<Integer, String> map = newLinkedHashMap(groupCount + 1); for (int i = 0; i <= groupCount; ++i) { //? String groupValue = matcher.group(i); //map.put(0, matcher.group());// ? 1 ?.0?,? m.group(0) m.group(). LOGGER.trace("matcher group[{}],start-end:[{}-{}],groupValue:[{}]", i, matcher.start(i), matcher.end(i), groupValue); map.put(i, groupValue);//groupValue } if (LOGGER.isTraceEnabled()) { LOGGER.trace("regexPattern:[{}],input:[{}],groupMap:{}", regexPattern, input, JsonUtil.format(map)); } return map; }
From source file:com.hangum.tadpole.engine.sql.parser.ddl.ParserDDL.java
/** * getObjectName//from w ww.j av a2 s.c o m * * @param matcher * @param sql * @return */ private String getObjectName(Matcher matcher, String sql) { int intEndIndex = matcher.end(1); int intContentLength = matcher.group(1).length(); if (logger.isDebugEnabled()) { logger.debug("===DDL Parser======================================"); logger.debug("SQL :" + sql); logger.debug("object name: " + matcher.group(1)); logger.debug("intContentLength : " + intContentLength); logger.debug("intEndIndex : " + intEndIndex); logger.debug("start index: " + (intEndIndex - intContentLength)); logger.debug("===DDL Parser======================================"); } String objctName = StringUtils.substring(sql, (intEndIndex - intContentLength), intEndIndex); objctName = StringUtils.remove(objctName, "\""); objctName = StringUtils.remove(objctName, "'"); objctName = StringUtils.remove(objctName, "`"); return objctName; }
From source file:com.hangum.tadpole.engine.sql.parser.UpdateDeleteParser.java
/** * getObjectName/*from w w w .j ava2s . c o m*/ * * @param matcher * @param sql * @return */ private String getObjectName(Matcher matcher, String sql) { int intEndIndex = matcher.end(1); int intContentLength = matcher.group(1).length(); // if(logger.isDebugEnabled()) { // logger.debug("===DDL Parser======================================"); // logger.debug("SQL :" + sql); // logger.debug("object name: " + matcher.group(1)); // logger.debug("intContentLength : " + intContentLength ); // logger.debug("intEndIndex : " + intEndIndex ); // logger.debug("start index: " + (intEndIndex - intContentLength)); // logger.debug("===DDL Parser======================================"); // } String objctName = StringUtils.substring(sql, (intEndIndex - intContentLength), intEndIndex); objctName = StringUtils.remove(objctName, "\""); objctName = StringUtils.remove(objctName, "'"); objctName = StringUtils.remove(objctName, "`"); return objctName; }
From source file:au.com.borner.salesforce.client.rest.domain.CompilerError.java
public Pair<Integer, Integer> getLocation() { String problem = getProblem(); Matcher matcher = pattern.matcher(problem); if (matcher.matches()) { int start = matcher.start(1); int end = matcher.end(1); Integer row = Integer.parseInt(problem.substring(start, end)); start = matcher.start(2);/*w ww.j av a 2 s .c o m*/ end = matcher.end(2); Integer column = Integer.parseInt(problem.substring(start, end)); adjustedProblem = problem.substring(end); return new Pair<Integer, Integer>(row, ++column); } else { return null; } }
From source file:com.edgenius.wiki.render.filter.HeadingFilter.java
public List<Region> getRegions(CharSequence input) { final List<Region> list = new ArrayList<Region>(); regexProvider.replaceByTokenVisitor(input, new TokenVisitor<Matcher>() { public void handleMatch(StringBuffer buffer, Matcher matcher) { int contentStart = matcher.start(4); int contentEnd = matcher.end(4); int start = contentStart; int end = contentEnd; list.add(new Region(HeadingFilter.this, false, start, end, contentStart, contentEnd)); }//from w ww . j a v a 2 s . c o m }); return list; }