Results 1 to 3 of 3
Thread: regular expressions in java
- 11-06-2012, 02:48 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 8
- Rep Power
- 0
regular expressions in java
Hello,
i would like to split words in comma, space and punctuations.I have write the above code
but when in the text i have something like these "322 abb",Java Code:import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer; // main class public class Main { // for saving word objects private ArrayList<Word> words; public static void main(String[] args) throws IOException { Main main = new Main(); main.start();//start } private void start() throws IOException { readFile(); // read file printWords(); // print results } private void readFile()throws IOException{ BufferedReader input = new BufferedReader(new FileReader("a.txt")); words = new ArrayList<Word>(); int lineNum = 1; // we read first line in start // delimeters of line in this example only "space" String delim1, delim2; char [] parse1 = {' '}; char [] parse2 = {','}; delim1 = new String(parse1); delim2 = new String(parse2); String line = input.readLine(); String [] lineWords; // read line while end of file while(line != null){ lineWords = line.split("[^a-zA-Z]+"); //lineWords = line.[\\s,;\n+\\d\\W\t\r\\s+]+ // split the words and create word object for (int i = 0; i < lineWords.length; i++) { if(lineWords[i]!=null){ System.out.print("i:"+i); //if(lineWords[i].compareTo(" ")!=0); System.out.println(lineWords[i]); Word w = new Word(lineNum,i+1,lineWords[i]); // lineNum -> line number i+1--> words index in line words.add(w); // lineWords[i] is word } } lineNum++; // pass the next line line = input.readLine(); } } // print out the results private void printWords() { System.out.println("WORD\t\tLINE\t\tINDEX"); System.out.println(); for (int i = 0; i < words.size(); i++) { Word w = words.get(i); if(w.name.compareTo("\\n")!=0) System.out.println(w.name + "\t\t" + w.lineNum + "\t\t" + w.index); } } // class word for model of every word object class Word{ private int lineNum; private int index; private String name; public Word(int lineNum, int index, String name) { this.lineNum = lineNum; this.index = index; this.name = name; } public int getLineNum() { return lineNum; } public int getIndex() { return index; } public String getName() { return name; } } }
the array linewords[], it returns a space at the first element (i=0) and then "abb".
Why it returns a space element?
- 11-07-2012, 03:06 PM #2
Member
- Join Date
- Nov 2012
- Posts
- 8
- Rep Power
- 0
Re: regular expressions in java
upppp
- 11-07-2012, 03:53 PM #3
Similar Threads
-
Java Regular Expressions: Comma Seperated List
By sgtblitz in forum New To JavaReplies: 3Last Post: 04-18-2011, 09:17 PM -
Regular Expressions Help
By Death Sickle in forum New To JavaReplies: 4Last Post: 04-04-2011, 04:21 AM -
regular expressions
By sozeee in forum New To JavaReplies: 3Last Post: 12-06-2010, 09:58 PM -
How to create regular expressions in Java
By maz09 in forum New To JavaReplies: 12Last Post: 04-02-2010, 05:13 PM -
Regular Expressions in java
By blue404 in forum Advanced JavaReplies: 2Last Post: 09-26-2008, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks