List of usage examples for java.util.regex Pattern DOTALL
int DOTALL
To view the source code for java.util.regex Pattern DOTALL.
Click Source Link
From source file:org.bibsonomy.scraper.url.kde.usenix.UsenixScraper.java
protected boolean scrapeInternal(ScrapingContext sc) throws ScrapingException { sc.setScraper(this); try {/* w w w . ja va2s .c o m*/ String path = sc.getUrl().getPath(); String title = null; String author = null; String event = null; String pages = null; String year = null; String key = null; /* * examples for current event/proceeding layout: * http://usenix.org/events/sec07/tech/drimer.html * http://usenix.org/publications/library/proceedings/tcl97/libes_writing.html * * TODO: * http://www.usenix.org/events/evt07/tech/full_papers/sandler/sandler_html/ */ String content = sc.getPageContent(); /* * Pattern */ // get year and key (event page) if (path.startsWith("/events/")) { // get year Pattern yearPattern = Pattern.compile(PATTERN_YEAR_EVENTS); Matcher yearMatcher = yearPattern.matcher(path); if (yearMatcher.find()) year = expandYear(yearMatcher.group(1)); //get key Pattern keyPattern = Pattern.compile(PATTERN_KEY_EVENTS); Matcher keyMatcher = keyPattern.matcher(path); if (keyMatcher.find()) key = keyMatcher.group(1); // get year and key (proceeding page) } else if (path.startsWith("/publications/library/proceedings/")) { // get year Pattern yearPattern = Pattern.compile(PATTERN_YEAR_PROCEEDING); Matcher yearMatcher = yearPattern.matcher(path); if (yearMatcher.find()) year = expandYear(yearMatcher.group(1)); //get key Pattern keyPattern = Pattern.compile(PATTERN_KEY_PROCEEDING); Matcher keyMatcher = keyPattern.matcher(path); if (keyMatcher.find()) key = keyMatcher.group(1); } // get title Pattern titlePattern = Pattern.compile(CURRENT_PATTERN_GET_TITLE, Pattern.DOTALL | Pattern.CASE_INSENSITIVE); Matcher titleMatcher = titlePattern.matcher(content); if (titleMatcher.find()) title = cleanup(titleMatcher.group(1), false); // get author Pattern authorPattern = Pattern.compile(CURRENT_PATTERN_GET_AUTHOR, Pattern.DOTALL | Pattern.CASE_INSENSITIVE); Matcher authorMatcher = authorPattern.matcher(content); if (authorMatcher.find()) author = cleanup(authorMatcher.group(1), true); else { /* * matching for different layout * example: http://usenix.org/publications/library/proceedings/ec96/geer.html */ authorPattern = Pattern.compile(CURRENT_WITH_BORDER_PATTERN_GET_AUTHOR, Pattern.DOTALL | Pattern.CASE_INSENSITIVE); authorMatcher = authorPattern.matcher(content); if (authorMatcher.find()) { author = cleanup(authorMatcher.group(1), true); author = author.replace("<HR>", ""); author = author.replace("<hr>", ""); author = author.replace("<P>", ""); author = author.replace("<p>", ""); // because of this: http://usenix.org/publications/library/proceedings/mob95/raja.html if (author.contains("<PRE>")) { authorPattern = Pattern.compile(OLD_PATTERN_GET_AUTHOR, Pattern.CASE_INSENSITIVE); authorMatcher = authorPattern.matcher(content); if (authorMatcher.find()) { author = cleanup(authorMatcher.group(1), true); author = author.replaceAll("\\s{2,}", " and "); } } } } if (author != null) { // replace "\n" with "and" author = author.replace("\n", " and "); // replace "," with "and" author = author.replace(",", " and "); // and cleanup while (author.contains("and and")) author = author.replaceAll("and\\s*and", "and"); if (author.endsWith(" and ")) author = author.substring(0, author.length() - 5); if (author.startsWith(" and ")) author = author.substring(5); } // get event Pattern eventPattern = Pattern.compile(CURRENT_PATTERN_GET_EVENT, Pattern.CASE_INSENSITIVE | Pattern.DOTALL); Matcher eventMatcher = eventPattern.matcher(content); if (eventMatcher.find()) { event = cleanup(eventMatcher.group(1), false); event = event.replace("\n", ""); } else { // old layout example: http://usenix.org/publications/library/proceedings/mob95/raja.html eventPattern = Pattern.compile(OLD_PATTERN_GET_EVENT, Pattern.CASE_INSENSITIVE | Pattern.DOTALL); eventMatcher = eventPattern.matcher(content); if (eventMatcher.find()) { event = cleanup(eventMatcher.group(1), false); event = event.replace("\n", ""); } } // get pages Pattern pagesPattern = Pattern.compile(CURRENT_PATTERN_GET_PAGES, Pattern.CASE_INSENSITIVE); Matcher pagesMatcher = pagesPattern.matcher(content); if (pagesMatcher.find()) pages = cleanup("Pp." + pagesMatcher.group(1), false); /* * TODO: may be abstract also * String abstract = null; */ StringBuffer resultBibtex = new StringBuffer(); if (key != null) resultBibtex.append("@inproceedings{" + key + ",\n"); else resultBibtex.append("@inproceedings{usenix,\n"); if (author != null) resultBibtex.append("\tauthor = {" + author + "},\n"); if (title != null) resultBibtex.append("\ttitle = {" + title + "},\n"); if (year != null) resultBibtex.append("\tyear = {" + year + "},\n"); if (event != null) resultBibtex.append("\tseries = {" + event + "},\n"); if (pages != null) resultBibtex.append("\tpages = {" + pages + "},\n"); String bibResult = resultBibtex.toString(); bibResult = bibResult.substring(0, bibResult.length() - 2) + "\n}\n"; // append url bibResult = BibTexUtils.addFieldIfNotContained(bibResult, "url", sc.getUrl().toString()); // add downloaded bibtex to result sc.setBibtexResult(bibResult); return true; } catch (UnsupportedEncodingException ex) { throw new InternalFailureException(ex); } }
From source file:nya.miku.wishmaster.ui.settings.AutohideActivity.java
@SuppressLint("InflateParams") @Override/*from w ww . j ava 2 s. c om*/ protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Object item = l.getItemAtPosition(position); final int changeId; if (item instanceof AutohideRule) { changeId = position - 1; } else { changeId = -1; //-1 - ? } Context dialogContext = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? new ContextThemeWrapper(this, R.style.Neutron_Medium) : this; View dialogView = LayoutInflater.from(dialogContext).inflate(R.layout.dialog_autohide_rule, null); final EditText regexEditText = (EditText) dialogView.findViewById(R.id.dialog_autohide_regex); final Spinner chanSpinner = (Spinner) dialogView.findViewById(R.id.dialog_autohide_chan_spinner); final EditText boardEditText = (EditText) dialogView.findViewById(R.id.dialog_autohide_boardname); final EditText threadEditText = (EditText) dialogView.findViewById(R.id.dialog_autohide_threadnum); final CheckBox inCommentCheckBox = (CheckBox) dialogView.findViewById(R.id.dialog_autohide_in_comment); final CheckBox inSubjectCheckBox = (CheckBox) dialogView.findViewById(R.id.dialog_autohide_in_subject); final CheckBox inNameCheckBox = (CheckBox) dialogView.findViewById(R.id.dialog_autohide_in_name); chanSpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, chans)); if (changeId != -1) { AutohideRule rule = (AutohideRule) item; regexEditText.setText(rule.regex); int chanPosition = chans.indexOf(rule.chanName); chanSpinner.setSelection(chanPosition != -1 ? chanPosition : 0); boardEditText.setText(rule.boardName); threadEditText.setText(rule.threadNumber); inCommentCheckBox.setChecked(rule.inComment); inSubjectCheckBox.setChecked(rule.inSubject); inNameCheckBox.setChecked(rule.inName); } else { chanSpinner.setSelection(0); } DialogInterface.OnClickListener save = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String regex = regexEditText.getText().toString(); if (regex.length() == 0) { Toast.makeText(AutohideActivity.this, R.string.autohide_error_empty_regex, Toast.LENGTH_LONG) .show(); return; } try { Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL); } catch (Exception e) { CharSequence message = null; if (e instanceof PatternSyntaxException) { String eMessage = e.getMessage(); if (!TextUtils.isEmpty(eMessage)) { SpannableStringBuilder a = new SpannableStringBuilder( getString(R.string.autohide_error_incorrect_regex)); a.append('\n'); int startlen = a.length(); a.append(eMessage); a.setSpan(new TypefaceSpan("monospace"), startlen, a.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); message = a; } } if (message == null) message = getString(R.string.error_unknown); Toast.makeText(AutohideActivity.this, message, Toast.LENGTH_LONG).show(); return; } AutohideRule rule = new AutohideRule(); int spinnerSelectedPosition = chanSpinner.getSelectedItemPosition(); rule.regex = regex; rule.chanName = spinnerSelectedPosition > 0 ? chans.get(spinnerSelectedPosition) : ""; // 0 ? = ? rule.boardName = boardEditText.getText().toString(); rule.threadNumber = threadEditText.getText().toString(); rule.inComment = inCommentCheckBox.isChecked(); rule.inSubject = inSubjectCheckBox.isChecked(); rule.inName = inNameCheckBox.isChecked(); if (!rule.inComment && !rule.inSubject && !rule.inName) { Toast.makeText(AutohideActivity.this, R.string.autohide_error_no_condition, Toast.LENGTH_LONG) .show(); return; } if (changeId == -1) { rulesJson.put(rule.toJson()); } else { rulesJson.put(changeId, rule.toJson()); } rulesChanged(); } }; AlertDialog dialog = new AlertDialog.Builder(this).setView(dialogView) .setTitle(changeId == -1 ? R.string.autohide_add_rule_title : R.string.autohide_edit_rule_title) .setPositiveButton(R.string.autohide_save_button, save) .setNegativeButton(android.R.string.cancel, null).create(); dialog.setCanceledOnTouchOutside(false); dialog.show(); }
From source file:org.etudes.util.HtmlHelper.java
/** * Remove image tags that have for src "file://" "webkit-fake-url://" or "x-apple-ql-id://" prefixes (transports) * //from w w w. j a v a 2 s . co m * @param data * the html data. * @return The cleaned up data. */ public static String stripBadImageTags(String data) { if (data == null) return data; // pattern to find link/meta tags // TODO: the .*? needs to stop on a >, else if there's a good image and later a bad one, it combines the two into one and removes it all! Pattern p = Pattern.compile("<img\\s+.*?src=\"(file:|webkit-fake-url:|x-apple-ql-id:).*?/>", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL); Matcher m = p.matcher(data); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, ""); } m.appendTail(sb); return sb.toString(); }
From source file:net.pms.util.OpenSubtitle.java
private static boolean login() throws IOException { tokenLock.writeLock().lock();/*www. j a v a 2 s.c o m*/ try { if (token != null && tokenIsYoung()) { return true; } URL url = new URL(OPENSUBS_URL); CredMgr.Cred cred = PMS.getCred("opensubtitles"); String pwd = ""; String usr = ""; if (cred != null) { // if we got credentials use them if (!StringUtils.isEmpty(cred.password)) { pwd = DigestUtils.md5Hex(cred.password); } usr = cred.username; } String req = "<methodCall>\n<methodName>LogIn</methodName>\n<params>\n" + "<param>\n<value><string>" + usr + "</string></value>\n</param>\n" + "<param>\n" + "<value><string>" + pwd + "</string></value>\n</param>\n<param>\n<value><string/></value>\n" + "</param>\n<param>\n<value><string>" + UA + "</string></value>\n</param>\n" + "</params>\n" + "</methodCall>\n"; Pattern re = Pattern.compile("token.*?<string>([^<]+)</string>", Pattern.DOTALL); Matcher m = re.matcher(postPage(url.openConnection(), req)); if (m.find()) { token = m.group(1); tokenAge = System.currentTimeMillis(); } return token != null; } finally { tokenLock.writeLock().unlock(); } }
From source file:TelnetTest.java
public void testFTP_Telnet_Remote_Execution_003() throws IOException { os.write("testexecute\r\n".getBytes()); os.flush();/*from www.ja v a 2s .c om*/ //this shell will lock, open another one and kill TEF TelnetClient tc = new TelnetClient(); tc.connect(telnetClient.getRemoteAddress()); InputStream is2 = tc.getInputStream(); OutputStream os2 = tc.getOutputStream(); readUntil(is2, prompt); os2.write("ps | grep TestExecute\r\n".getBytes()); os2.flush(); String s = readUntil(is2, prompt); Pattern p = Pattern.compile(".*\\D+(\\d+)\\s[\\s:\\d]+TestExecute Script Engine.*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); //Pattern.DOTALL => '.' includes end of line Matcher m = p.matcher(s); assertTrue(m.matches()); String s1 = m.group(1); int pid = Integer.parseInt(s1); os2.write(("kill " + pid + "\r\n").getBytes()); os2.flush(); readUntil(is2, prompt); os2.write("bye\r\n".getBytes()); os2.flush(); //we should be able now to close the other shell readUntil(is, prompt); }
From source file:com.github.rwitzel.streamflyer.regex.RegexModifierTest.java
@Test public void testExampleFromHomepage_advancedExample_secondImprovement() throws Exception { // first improvement Modifier myModifier1 = new RegexModifier("edit\\sstream", Pattern.DOTALL, "modify stream"); // test: does not preserve type of whitespace characters assertNotEquals("modify\tstream", modify("edit\tstream", myModifier1)); // test: does not support many whitespace characters assertNotEquals("modify stream", modify("edit stream", myModifier1)); // second improvement Modifier myModifier2 = new RegexModifier("edit(\\s++stream)", Pattern.DOTALL, "modify$1"); // test: preserves type of whitespace characters assertEquals("modify\tstream", modify("edit\tstream", myModifier2)); // test: supports many whitespace characters assertEquals("modify stream", modify("edit stream", myModifier2)); }
From source file:com.netflix.imfutility.conversion.executor.ConversionOperationParser.java
private String removeQuotes(String param) { Pattern p = Pattern.compile(QUOTES_TEMPLATE, Pattern.DOTALL); if (p.matcher(param).matches()) { return param.substring(1, param.length() - 1); }//from w w w . j a v a 2 s .c o m return param; }
From source file:org.owasp.dependencycheck.analyzer.SwiftPackageManagerAnalyzer.java
/** * Extracts evidence from the package description and adds it to the given * evidence collection./*from w ww.j av a 2s .c o m*/ * * @param dependency the dependency being analyzed * @param type the type of evidence to add * @param packageDescription the text to extract evidence from * @param field the name of the field being searched for * @param fieldPattern the field pattern within the contents to search for * @param confidence the confidence level of the evidence if found * @return the string that was added as evidence */ private String addStringEvidence(Dependency dependency, EvidenceType type, String packageDescription, String field, String fieldPattern, Confidence confidence) { String value = ""; final Matcher matcher = Pattern.compile(String.format("%s *:\\s*\"([^\"]*)", fieldPattern), Pattern.DOTALL) .matcher(packageDescription); if (matcher.find()) { value = matcher.group(1); } if (value != null) { value = value.trim(); if (value.length() > 0) { dependency.addEvidence(type, SPM_FILE_NAME, field, value, confidence); } } return value; }
From source file:com.etime.ETimeUtils.java
/** * Adds all Punches in a given string to a list of punches. * * @param row The string to be searched for punches. * @param punches A list of punches to be added to. *//*from w w w .j av a 2s .c om*/ private static void addPunchesFromRowToList(String row, List<Punch> punches) { //Format to be matched is // <td title="" class="InPunch"><div class="" title=""> 2:45PM </div></td> Pattern punchPattern = Pattern.compile("(?i)((InPunch|OutPunch)\">)(.*?)(>\\s*)(.*?)(\\s*</div>)", Pattern.MULTILINE | Pattern.DOTALL); Matcher punchMatcher = punchPattern.matcher(row); Punch punch; while (punchMatcher.find()) { String punchStr = punchMatcher.group(5); if (!punchStr.equals(" ")) { punch = getPunchFromString(punchStr);//parse a Punch from a string (e.g. "12:00PM") if (punchMatcher.group(2).equals("InPunch")) { punch.setClockIn(true); } else { punch.setClockIn(false); } punches.add(punch); } } }
From source file:org.etudes.mneme.impl.ImporteCollegeTextServiceImpl.java
public void importQuestions(String context, Pool pool, String text) throws AssessmentPermissionException { if ((text == null) || (text.length() == 0)) return;//w w w. j a va2 s . c o m // replace any \r\n with just a \n text = text.replaceAll("\r\n", "\n"); String title = "eCollege paste"; Float points = new Float("1"); if (pool == null) { pool = this.poolService.newPool(context); //read title from the first line ex: Unit 2: Week 2 - Quiz String findTitle = text.substring(0, text.indexOf("\n")); if (findTitle != null) { String[] titleParts = findTitle.split("[:-]"); if (titleParts.length == 2 && titleParts[1] != null && titleParts[1].length() != 0) title = titleParts[1].trim(); else if (titleParts.length > 2) title = findTitle.substring(findTitle.indexOf(titleParts[1])); } pool.setTitle(title); pool.setPointsEdit(points); // create assessment Assessment assmt = assessmentService.newAssessment(context); assmt.setType(AssessmentType.test); assmt.setTitle(title); Part part = assmt.getParts().addPart(); Pattern p_groups = Pattern.compile("Collapse[\\s]*Question(.*?)[\\n]*[\\t]*row[\\t]*Move[\\s]*Question", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL); Matcher m = p_groups.matcher(text); StringBuffer sb = new StringBuffer(); while (m.find()) { String workOn = m.group(0); String[] lines = workOn.split("[\\n]"); processECollegeTextGroup(pool, part, lines); m.appendReplacement(sb, ""); } m.appendTail(sb); // remaining last text if (sb != null && sb.length() != 0) { if (sb.indexOf("Collapse Question") != -1) { String workOn = sb.substring(sb.indexOf("Collapse Question")); String[] lines = workOn.split("[\\n]"); processECollegeTextGroup(pool, part, lines); } } try { assmt.getGrading().setGradebookIntegration(Boolean.TRUE); if (assmt.getParts().getTotalPoints().floatValue() <= 0) { assmt.setNeedsPoints(Boolean.FALSE); } assessmentService.saveAssessment(assmt); } catch (AssessmentPolicyException ep) { } this.poolService.savePool(pool); } }