Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Question regular expression

    hi all..

    here is a program.......

    import java.util.regex.*;
    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.nio.charset.*;

    public class demo{
    public static void main(String[] args) throws IOException{

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter file name for search: ");
    String filename = in.readLine();
    File file = new File(filename);
    if(!filename.endsWith(".txt")){
    System.out.println("This is not a text file.");
    System.out.println("Please enter file name withe \".txt\" extension.");
    System.exit(0);
    }
    else if(!file.exists()){
    System.out.println("File not found.");
    System.exit(0);
    }

    FileChannel fileChannel = new FileInputStream(filename).getChannel();
    CharBuffer charBuffer = Charset.forName("8859_1").newDecoder().decode(
    fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int)fileChannel.size()));
    Pattern pattern = Pattern.compile("\\s[0-9]?[0-9]\\s");
    Matcher matcher = pattern.matcher(charBuffer);
    String b="5";
    int a = 0;
    while(matcher.find()){
    a++;
    String matchStrings = matcher.group();
    String []splits = matchStrings.split(" ");
    for(int i=0; i< splits.length ;i++){

    System.out.println(splits[i]);
    }


    for(int j=0; j< splits.length ;j++){

    if (splits[j].trim().equals(b)) {
    System.out.println("The strings are equal.");
    } else {
    System.out.println("The strings are unequal.");
    }


    }




    }
    System.out.println("Total occurance of the word in the file is: " + a);
    }
    }

    IN this program First I have read a file that I have attach ..i.e "prob.txt" and parse the file according to regular expression and extract 1,2,3...........50 number on prob.txt...and now I want to extract two value given in the bracket (3.84) and [6.64] corresponding to number that we have previosly extract.for example if we have number 1 come then ,extact two value i.e (3.84) and [6.64] corresponding to number 1.other example if we have number 2 come then ,extact two value i.e (5.99) and [9.21] corresponding to number 2.I hope u all understand.plz solve this problem.Thanx in advance..
    Attached Files Attached Files

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Just create a record class that holds the variables
    int line;
    double first;
    double second;
    Then you read the file line by line creating a List<Record>. All you need to do at each line is String.split by "\\s+".

  3. #3
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Exclamation reg

    hi......
    sorry I could not understand...can u elobarate this????????

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Which part did you not understand?

  5. #5
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Question regular expression

    THanx for ur reply
    I am not able to understand what is the need of creating a class?????????

    Second thing.....how I am able to extract the corresponding value against 1,2

    Plz explainin detail as I have not done file reading and use regular expression before????????

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    When programming in Java (or any object oriented language) always think objects and classes. Problem solving will be easy that way.

    You should be reading that file using FileReader/BufferedReader since it contains text data. You read a line using the readLine() method. Then you split each line into tokens using the regex I gave you above. The split returns an array (Read about it in the API specs) with the tokens in the array. The line numbers are at position 0. You should be able to work out where the rest of the tokens are out.

  7. #7
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    try{
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream("prob.txt");
    // Get the object of DataInputStream
    DataInputStream inn = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(inn));
    String strLine;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null) {
    // Print the content on the console
    //String []splits1 = strLine.split(" \\s+." );
    String []splits1 = strLine.split("\\}+." );
    for(int j=0; j< splits1.length ;j++){
    System.out.println(splits1[j]);
    }

    }
    //Close the input stream
    in.close();
    }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    now I have written this code................I made an array in which each line of txt file will store...now what to do next if I want to take out from each line.....value given in bracket for ex.(3.84)and [6.64]

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You didn't read my response properly.
    Do not use those classes for reading character files. See the classes I mentioned above.
    Also re-read all the responses I posted and see if your implementation matches them.

  9. #9
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Java Code:
    Pattern patter = Pattern.compile("([\\w]+)([\\(\\[])([0-9|\\d\\.0-9])([0-9])([\\)\\]])([\\w])");
    Extract from this regexp groups: $3$4

  10. #10
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    i have used that classes as u said..........but that's not working too....Plz tell me what else I can do????

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Post the classes that you created (using code tags).

  12. #12
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    File file1 = new File("prob.txt");
    StringBuffer contents = new StringBuffer();
    BufferedReader reader = null;

    <code> try {
    reader = new BufferedReader(new FileReader(file1));
    String text = null;

    // repeat until all lines are read
    while ((text = reader.readLine()) != null) {




    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if (reader != null) {
    reader.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    </code>

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Now compare that with the suggestions I gave in my first reply.

  14. #14
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Question

    hi..
    Thanx for ur reply.........u have ask for creating a class ...........means I create a class which hold each line of file...Is that u want to say????

  15. #15
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Yes, I've even told you the name of the class and the name of the instance variables to put in the class.

  16. #16
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    thanx...but one thing why u have used int line the double first...means different datatype.....can i store each line of file in string variable????

  17. #17
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    That's what your requirements say, you wrote
    1,2,3...........50 number on prob.txt...and now I want to extract two value given in the bracket (3.84) and [6.64] corresponding to number that we have previosly extract.for example if we have number 1 come then ,extact two value i.e (3.84) and [6.64] corresponding to number 1

  18. #18
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Question

    hi..
    Can u tell me one thing that can I store each line in a string variable??

  19. #19
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Yes and you will initially. Then you will split that line using the regex I've posted and use the generated pieces to create a Record which you will add to an ArrayList. At the end of the process you will have a List<Record> with the values you want.

  20. #20
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    ry{
    File file1 = new File("prob.txt");
    StringBuffer contents = new StringBuffer();
    BufferedReader reader = null;

    try {
    reader = new BufferedReader(new FileReader(file1));
    String text = null;

    // repeat until all lines are read
    while ((strLine = br.readLine()) != null) {

    String []splits1 = strLine.split(" \\s+." );

    for(int j=0; j< splits1.length ;j++){
    System.out.println(splits1[j]);


    }
    from this code each line splits....ok....but now how take out from each line 1,(3.84) and [6.64].I know u have said to use class ...but how to read 1,(3.84) and[6.64] and put in a variable

Page 1 of 2 12 LastLast

Similar Threads

  1. need help to make this regular expression
    By aruna1 in forum New To Java
    Replies: 1
    Last Post: 04-12-2009, 08:09 PM
  2. Regular Expression Problem
    By daflores in forum Advanced Java
    Replies: 8
    Last Post: 02-10-2009, 06:45 PM
  3. Complex Regular Expression HELP
    By hiklior in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 01:52 PM
  4. Regular Expression Challange
    By hiklior in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 05:05 AM
  5. Regular expression with Intersections
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 12:03 PM

Posting Permissions

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