List of usage examples for java.util.regex Pattern MULTILINE
int MULTILINE
To view the source code for java.util.regex Pattern MULTILINE.
Click Source Link
From source file:MainClass.java
public static void main(String[] args) { Pattern p = Pattern.compile("^java", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); Matcher m = p.matcher("java has regex\nJava has regex\n" + "JAVA has pretty good regular expressions\n" + "Regular expressions are in Java"); while (m.find()) System.out.println(m.group()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { CharSequence inputStr = "abc\ndef"; String patternStr = "abc$"; // Compile with multiline enabled Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.find(); // true // Use an inline modifier to enable multiline mode matchFound = pattern.matches(".*abc$.*", "abc\r\ndef"); // false matchFound = pattern.matches("(?m).*abc$.*", "abc\r\ndef"); // true }
From source file:Main.java
public static void main(String[] argv) throws Exception { CharSequence inputStr = "a\rb"; inputStr = "a\r\nb"; inputStr = "a\nb"; String patternStr = "^(.*)$"; Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE); Matcher matcher = pattern.matcher(inputStr); while (matcher.find()) { String lineWithTerminator = matcher.group(0); String lineWithoutTerminator = matcher.group(1); }//from ww w . j a va2 s . c o m }
From source file:Main.java
public static void main(String[] argv) throws Exception { CharSequence inputStr = "a\r\rb"; // Mac //inputStr = "a\r\n\r\nb"; // Windows //inputStr = "a\n\nb"; // Unix String patternStr = "(?<=(\r\n|\r|\n))([ \\t]*$)+"; String[] paras = Pattern.compile(patternStr, Pattern.MULTILINE).split(inputStr); for (int i = 0; i < paras.length; i++) { String paragraph = paras[i]; }//from w w w. j av a 2 s.co m }
From source file:Main.java
public static void main(String[] argv) throws Exception { CharSequence inputStr = "a\r\rb"; // Mac //inputStr = "a\r\n\r\nb"; // Windows //inputStr = "a\n\nb"; // Unix String patternStr = "(^.*\\S+.*$)+"; Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE); Matcher matcher = pattern.matcher(inputStr); while (matcher.find()) { String paragraph = matcher.group(); }//from w w w . j a va 2 s.co m }
From source file:WordCount.java
public static void main(String args[]) throws Exception { String filename = "WordCount.java"; // Map File from filename to byte buffer FileInputStream input = new FileInputStream(filename); FileChannel channel = input.getChannel(); int fileLength = (int) channel.size(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileLength); // Convert to character buffer Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharBuffer charBuffer = decoder.decode(buffer); // Create line pattern Pattern linePattern = Pattern.compile(".*$", Pattern.MULTILINE); // Create word pattern Pattern wordBreakPattern = Pattern.compile("[\\p{Punct}\\s}]"); // Match line pattern to buffer Matcher lineMatcher = linePattern.matcher(charBuffer); Map map = new TreeMap(); Integer ONE = new Integer(1); // For each line while (lineMatcher.find()) { // Get line CharSequence line = lineMatcher.group(); // Get array of words on line String words[] = wordBreakPattern.split(line); // For each word for (int i = 0, n = words.length; i < n; i++) { if (words[i].length() > 0) { Integer frequency = (Integer) map.get(words[i]); if (frequency == null) { frequency = ONE;/*from w w w .j a va 2 s .c o m*/ } else { int value = frequency.intValue(); frequency = new Integer(value + 1); } map.put(words[i], frequency); } } } System.out.println(map); }
From source file:utybo.branchingstorytree.swing.utils.FontTF.java
public static void main(String[] args) throws MalformedURLException, IOException { Scanner sc = new Scanner(System.in); System.out.print("Path to transform from => "); String s = sc.nextLine();/* www .j a v a 2 s .com*/ System.out.print("Save to => "); File f = new File(sc.nextLine()); f.createNewFile(); System.out.println("Downloading..."); String string = IOUtils.toString(new FileInputStream(new File(s)), StandardCharsets.UTF_8); System.out.println(string); System.out.println("Transforming"); Pattern p = Pattern.compile("local\\(.*?\\), local\\(.*?\\), url\\((http.+?)\\)", Pattern.MULTILINE); Matcher m = p.matcher(string); while (m.find()) { String toReplace = m.group(); String url = m.group(1); System.out.println("Downloading " + url); byte[] raw = IOUtils.toByteArray(new URL(url).openStream()); System.out.println("Converting"); String b64 = Base64.getEncoder().encodeToString(raw); string = string.replace(toReplace, "url(data:font/woff2;charset=utf-8;base64," + b64 + ")"); System.out.println(string); m.reset(string); } System.out.println("Writing"); FileUtils.write(f, string, StandardCharsets.UTF_8); System.out.println("Done"); sc.close(); }
From source file:BGrep.java
public static void main(String[] args) { String encodingName = "UTF-8"; // Default to UTF-8 encoding int flags = Pattern.MULTILINE; // Default regexp flags try { // Fatal exceptions are handled after this try block // First, process any options int nextarg = 0; while (args[nextarg].charAt(0) == '-') { String option = args[nextarg++]; if (option.equals("-e")) { encodingName = args[nextarg++]; } else if (option.equals("-i")) { // case-insensitive matching flags |= Pattern.CASE_INSENSITIVE; } else if (option.equals("-s")) { // Strict Unicode processing flags |= Pattern.UNICODE_CASE; // case-insensitive Unicode flags |= Pattern.CANON_EQ; // canonicalize Unicode } else { System.err.println("Unknown option: " + option); usage();//from ww w . ja va2s.co m } } // Get the Charset for converting bytes to chars Charset charset = Charset.forName(encodingName); // Next argument must be a regexp. Compile it to a Pattern object Pattern pattern = Pattern.compile(args[nextarg++], flags); // Require that at least one file is specified if (nextarg == args.length) usage(); // Loop through each of the specified filenames while (nextarg < args.length) { String filename = args[nextarg++]; CharBuffer chars; // This will hold complete text of the file try { // Handle per-file errors locally // Open a FileChannel to the named file FileInputStream stream = new FileInputStream(filename); FileChannel f = stream.getChannel(); // Memory-map the file into one big ByteBuffer. This is // easy but may be somewhat inefficient for short files. ByteBuffer bytes = f.map(FileChannel.MapMode.READ_ONLY, 0, f.size()); // We can close the file once it is is mapped into memory. // Closing the stream closes the channel, too. stream.close(); // Decode the entire ByteBuffer into one big CharBuffer chars = charset.decode(bytes); } catch (IOException e) { // File not found or other problem System.err.println(e); // Print error message continue; // and move on to the next file } // This is the basic regexp loop for finding all matches in a // CharSequence. Note that CharBuffer implements CharSequence. // A Matcher holds state for a given Pattern and text. Matcher matcher = pattern.matcher(chars); while (matcher.find()) { // While there are more matches // Print out details of the match System.out.println(filename + ":" + // file name matcher.start() + ": " + // character pos matcher.group()); // matching text } } } // These are the things that can go wrong in the code above catch (UnsupportedCharsetException e) { // Bad encoding name System.err.println("Unknown encoding: " + encodingName); } catch (PatternSyntaxException e) { // Bad pattern System.err.println("Syntax error in search pattern:\n" + e.getMessage()); } catch (ArrayIndexOutOfBoundsException e) { // Wrong number of arguments usage(); } }
From source file:com.lehman.ic9.ic9Main.java
/** * Program entry point.//from ww w . j a v a 2s .c o m * @param args is an array of strings from the OS. */ public static void main(String[] args) { try { if (args.length >= 1) { if (parseScriptArgs(args)) { if (file.exists(script)) { // Load dependent jars in assemblyPath/lib-depends directory. jarLoader.getInstance().loadJarsInPathRecursively(sys.getAssemblyPath() + "lib-depends", debug); // Load dependent jars in assemblyPath/lib directory if any. jarLoader.getInstance().loadJarsInPathRecursively(sys.getAssemblyPath() + "lib", debug); // Create the Ic9 engine and eval the script. String[] engArgs = { "-scripting" }; ic9engine eng = new ic9engine(engArgs); eng.setMainArgs(script, scriptArgs); // Remove shebang if it exists. String contents = Pattern.compile("^#!/.*?$", Pattern.MULTILINE).matcher(file.read(script)) .replaceAll(""); eng.eval(script, contents); // If -t flag, attempt to call test() function. if (runTest) { eng.runTest(); } } else { System.err.println("File '" + script + "' couldn't be found."); } } } else { runInteractive(); } } catch (ScriptException e) { e.printStackTrace(); } catch (ic9exception e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
From source file:NLMatch.java
public static void main(String[] argv) { String input = "I dream of engines\nmore engines, all day long"; System.out.println("INPUT: " + input); System.out.println();/* w ww. j a va2 s. c om*/ String[] patt = { "engines.more engines", "engines$" }; for (int i = 0; i < patt.length; i++) { System.out.println("PATTERN " + patt[i]); boolean found; Pattern p1l = Pattern.compile(patt[i]); found = p1l.matcher(input).find(); System.out.println("DEFAULT match " + found); Pattern pml = Pattern.compile(patt[i], Pattern.DOTALL | Pattern.MULTILINE); found = pml.matcher(input).find(); System.out.println("MultiLine match " + found); System.out.println(); } }