Results 1 to 2 of 2
  1. #1
    meta1203 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Angry Regular Expressions and String.replaceAll()

    Hello! This is my first post, so just trying to see how this turns out...
    I am writing an NBT (Named Binary Tag) reader. I would like to remove all the double newlines. So:
    Java Code:
    Hi
    
    Hi again
    will be
    Java Code:
    Hi
    Hi again
    Here is my code (I am using the JNBT library, look it up if you want it):
    IO.java
    Java Code:
    import java.io.*;
    public class IO {
    
    
    public String strIn() {
    BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
    String input = null;
    try {
    input = console.readLine();
    }
    catch (IOException e) { input = "<" + e + ">"; }
    return input;
    }
    
    public double numIn()
    {
    BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
    String input = null;
    try {
    input = console.readLine();
    }
    catch (IOException e) { input = "<" + e + ">"; }
    
    return Double.parseDouble(input);
    
    }
    
    public void out(String ut) {
    System.out.println(ut);
    }
    
    public void out(int ut) {
    String go = Integer.toString(ut);
    out(go);
    }
    
    public void out(double ut) {
    String go = Double.toString(ut);
    out(go);
    }
    
    }
    NBT.java
    Java Code:
    import org.jnbt.*;
    import java.io.*;
    
    public class NBT {
    
    public static void main(String[] args) {
    	// Terminal input
    	IO io = new IO();
    	
    	// Get Directory and Filename of file
    	NBTInputStream in = null;
    	System.out.println("Directory:");
    	String dir = io.strIn();
    	System.out.println("File:");
    	String file = io.strIn();
    	File f = new File(dir,file);
    	
    	// Get whole data
    	try {
    		in = new NBTInputStream(new FileInputStream(f));
    	}
    	catch (IOException e) {
    		System.out.println("Opps...");
    		System.exit(0);
    	}
    	
    	Tag t = null;
    	try {
    		t = in.readTag();
    	}
    	catch (IOException e) {
    		System.out.println("ARG! "+e);
    		System.exit(0);
    	}
    	
    	String whole = t.toString();
    	// Excess stuff Removal
    	whole = whole.replaceAll("[{]", "");
    	whole = whole.replaceAll("[}]", "");
    	whole = whole.replaceAll("[ ]", "");
    	whole = whole.replaceAll("entries", "");
    	whole = whole.replaceAll("oftype", "");
    	// Here is where I am stuck!
    	whole = whole.replaceAll("$\n", "\n");
    	
    	String[] line = null;
    	// Split whole data into lines
    	line = whole.split("\n");
    	// Print out whole data
    	System.out.println(whole);
    	int x = 0;
    	// Split lines into definers and data
    	
    	String[][] data = new String[java.lang.reflect.Array.getLength(line)][];
    	
    	while (x<java.lang.reflect.Array.getLength(line)) {
    		data[x]=line[x].split("[:]");
    		x=x+1;
    	} 
    	System.out.println(data[2][0]);
    	System.out.println(data[2][1]);
    }
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    10

Similar Threads

  1. Using regular expressions for search
    By carderne in forum New To Java
    Replies: 9
    Last Post: 05-24-2009, 04:58 AM
  2. Regular Expressions in java
    By blue404 in forum Advanced Java
    Replies: 2
    Last Post: 09-26-2008, 03:43 AM
  3. Using Quantifiers in regular expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-10-2008, 10:43 AM
  4. Regular expressions quantifiers
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 11:18 AM
  5. regular expressions and string matching
    By DennyLoi in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 10:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •